diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 81d342e7..a4fbc025 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1387,6 +1387,15 @@ linters-settings: # Optimizes even if it requires an int or uint type cast. # Default: true int-conversion: false + # Optimizes into `err.Error()` even if it is only equivalent for non-nil errors. + # Default: false + err-error: true + # Optimizes `fmt.Errorf`. + # Default: true + errorf: false + # Optimizes `fmt.Sprintf` with only one argument + # Default: true + sprintf1: false prealloc: # IMPORTANT: we don't recommend using this linter before doing performance profiling. diff --git a/go.mod b/go.mod index 2bb5c0cf..4a11cce0 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( github.com/breml/errchkjson v0.3.6 github.com/butuzov/ireturn v0.2.2 github.com/butuzov/mirror v1.1.0 - github.com/catenacyber/perfsprint v0.3.0 + github.com/catenacyber/perfsprint v0.3.1 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.11.2 diff --git a/go.sum b/go.sum index b991df1a..1865cd1b 100644 --- a/go.sum +++ b/go.sum @@ -102,8 +102,8 @@ github.com/butuzov/ireturn v0.2.2 h1:jWI36dxXwVrI+RnXDwux2IZOewpmfv930OuIRfaBUJ0 github.com/butuzov/ireturn v0.2.2/go.mod h1:RfGHUvvAuFFxoHKf4Z8Yxuh6OjlCw1KvR2zM1NFHeBk= github.com/butuzov/mirror v1.1.0 h1:ZqX54gBVMXu78QLoiqdwpl2mgmoOJTk7s4p4o+0avZI= github.com/butuzov/mirror v1.1.0/go.mod h1:8Q0BdQU6rC6WILDiBM60DBfvV78OLJmMmixe7GF45AE= -github.com/catenacyber/perfsprint v0.3.0 h1:xMciPd+OYZd2oWJhoqBlnu4Vfe284ktxDZHQkmdjNrU= -github.com/catenacyber/perfsprint v0.3.0/go.mod h1:/wclWYompEyjUD2FuIIDVKNkqz7IgBIWXIH3V0Zol50= +github.com/catenacyber/perfsprint v0.3.1 h1:KGTSplWrKftfyqUrXAlk28z7HyoJjZWgvbjwv05fSIw= +github.com/catenacyber/perfsprint v0.3.1/go.mod h1:/wclWYompEyjUD2FuIIDVKNkqz7IgBIWXIH3V0Zol50= github.com/ccojocar/zxcvbn-go v1.0.1 h1:+sxrANSCj6CdadkcMnvde/GWU1vZiiXRbqYSCalV4/4= github.com/ccojocar/zxcvbn-go v1.0.1/go.mod h1:g1qkXtUSvHP8lhHp5GrSmTz6uWALGRMQdw6Qnz/hi60= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index f6687db8..768d82df 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -106,6 +106,9 @@ var defaultLintersSettings = LintersSettings{ }, PerfSprint: PerfSprintSettings{ IntConversion: true, + ErrError: false, + ErrorF: true, + SprintF1: true, }, Prealloc: PreallocSettings{ Simple: true, @@ -691,6 +694,9 @@ type ParallelTestSettings struct { type PerfSprintSettings struct { IntConversion bool `mapstructure:"int-conversion"` + ErrError bool `mapstructure:"err-error"` + ErrorF bool `mapstructure:"errorf"` + SprintF1 bool `mapstructure:"sprintf1"` } type PreallocSettings struct { diff --git a/pkg/golinters/perfsprint.go b/pkg/golinters/perfsprint.go index 4549501c..a74fd9fa 100644 --- a/pkg/golinters/perfsprint.go +++ b/pkg/golinters/perfsprint.go @@ -16,6 +16,9 @@ func NewPerfSprint(settings *config.PerfSprintSettings) *goanalysis.Linter { cfg = map[string]map[string]any{ a.Name: { "int-conversion": settings.IntConversion, + "err-error": settings.ErrError, + "errorf": settings.ErrorF, + "sprintf1": settings.SprintF1, }, } } diff --git a/test/testdata/configs/perfsprint_custom.yml b/test/testdata/configs/perfsprint_custom.yml new file mode 100644 index 00000000..f813f99a --- /dev/null +++ b/test/testdata/configs/perfsprint_custom.yml @@ -0,0 +1,7 @@ +linters-settings: + perfsprint: + int-conversion: false + err-error: true + errorf: false + sprintf1: false + diff --git a/test/testdata/configs/perfsprint_int_conversion.yml b/test/testdata/configs/perfsprint_int_conversion.yml deleted file mode 100644 index 4a8fb20e..00000000 --- a/test/testdata/configs/perfsprint_int_conversion.yml +++ /dev/null @@ -1,3 +0,0 @@ -linters-settings: - perfsprint: - int-conversion: false diff --git a/test/testdata/perfsprint.go b/test/testdata/perfsprint.go index be369338..8799f8f1 100644 --- a/test/testdata/perfsprint.go +++ b/test/testdata/perfsprint.go @@ -15,10 +15,10 @@ func TestPerfsprint() { ui uint ) - fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string" - fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string" - fmt.Sprintf("%s", err) // want "fmt.Sprintf can be replaced with err.Error()" - fmt.Sprint(err) // want "fmt.Sprint can be replaced with err.Error()" + fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string" + fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string" + fmt.Sprintf("%s", err) + fmt.Sprint(err) fmt.Sprintf("%t", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool" fmt.Sprint(b) // want "fmt.Sprint can be replaced with faster strconv.FormatBool" fmt.Sprintf("%d", i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa" @@ -33,9 +33,9 @@ func TestPerfsprint() { fmt.Sprint("test", 42) fmt.Sprint(42, 42) - fmt.Sprintf("test") - fmt.Sprintf("%v") - fmt.Sprintf("%d") + fmt.Sprintf("test") // want "fmt.Sprintf can be replaced with just using the string" + fmt.Sprintf("%v") // want "fmt.Sprintf can be replaced with just using the string" + fmt.Sprintf("%d") // want "fmt.Sprintf can be replaced with just using the string" fmt.Sprintf("%d", 42, 42) fmt.Sprintf("%#d", 42) fmt.Sprintf("value %d", 42) diff --git a/test/testdata/perfsprint_int_conversion.go b/test/testdata/perfsprint_custom.go similarity index 96% rename from test/testdata/perfsprint_int_conversion.go rename to test/testdata/perfsprint_custom.go index 7f0a50ff..046cc94f 100644 --- a/test/testdata/perfsprint_int_conversion.go +++ b/test/testdata/perfsprint_custom.go @@ -1,5 +1,5 @@ //golangcitest:args -Eperfsprint -//golangcitest:config_path testdata/configs/perfsprint_int_conversion.yml +//golangcitest:config_path testdata/configs/perfsprint_custom.yml package testdata import (