Apply the stringscutprefix modernizer (#1051)

The stringscutprefix analyzer simplifies a common pattern where code first
checks for a prefix with `strings.HasPrefix` and then removes it with
`strings.TrimPrefix`. It replaces this two-step process with a single
call to `strings.CutPrefix`, introduced in Go 1.20. The analyzer also
handles the equivalent functions in the `bytes` package.

See https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#hdr-Analyzer_stringscutprefix
This commit is contained in:
James Raspass 2026-02-19 13:12:48 +00:00 committed by GitHub
parent be60b2ec57
commit 3328d4d3ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -70,8 +70,8 @@ func NewCompressor(level int, types ...string) *Compressor {
if strings.Contains(strings.TrimSuffix(t, "/*"), "*") {
panic(fmt.Sprintf("middleware/compress: Unsupported content-type wildcard pattern '%s'. Only '/*' supported", t))
}
if strings.HasSuffix(t, "/*") {
allowedWildcards[strings.TrimSuffix(t, "/*")] = struct{}{}
if before, ok := strings.CutSuffix(t, "/*"); ok {
allowedWildcards[before] = struct{}{}
} else {
allowedTypes[t] = struct{}{}
}