diff --git a/pkg/commands/run.go b/pkg/commands/run.go index b4cd88f3b54f..15cd670e11e6 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -176,6 +176,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is fs.IntVar(&lsc.Goconst.NumberMax, "goconst.max", 3, "maximum value, only works with goconst.numbers") hideFlag("goconst.max") + fs.BoolVar(&lsc.Goconst.IgnoreCalls, "goconst.ignore-calls", + true, "Goconst: ignore when constant is not used as function argument") + hideFlag("goconst.ignore-calls") // (@dixonwille) These flag is only used for testing purposes. fs.StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil, diff --git a/pkg/config/config.go b/pkg/config/config.go index 2fa20eeea42d..4ec378f7a687 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -202,6 +202,7 @@ type LintersSettings struct { ParseNumbers bool `mapstructure:"numbers"` NumberMin int `mapstructure:"min"` NumberMax int `mapstructure:"max"` + IgnoreCalls bool `mapstructure:"ignore-calls"` } Gomnd struct { Settings map[string]map[string]interface{} diff --git a/pkg/golinters/goconst.go b/pkg/golinters/goconst.go index 39e967055da4..cc8213effe21 100644 --- a/pkg/golinters/goconst.go +++ b/pkg/golinters/goconst.go @@ -66,7 +66,10 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis. res := make([]goanalysis.Issue, 0, len(goconstIssues)) for _, i := range goconstIssues { - textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount) + if lintCtx.Settings().Goconst.IgnoreCalls && i.Typ == goconstAPI.Call { + continue + } + textBegin := fmt.Sprintf("string %s has %d occurrences as %s statement", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount, i.Typ) var textEnd string if i.MatchingConst == "" { textEnd = ", make it a constant" diff --git a/test/testdata/goconst.go b/test/testdata/goconst.go index aaebe1dcd013..2fc1a7aae0b6 100644 --- a/test/testdata/goconst.go +++ b/test/testdata/goconst.go @@ -4,7 +4,7 @@ package testdata import "fmt" func GoconstA() { - a := "needconst" // ERROR "string `needconst` has 5 occurrences, make it a constant" + a := "needconst" // ERROR "string `needconst` has 5 occurrences as assignment statement, make it a constant" fmt.Print(a) b := "needconst" fmt.Print(b) @@ -22,10 +22,11 @@ func GoconstB() { const AlreadyHasConst = "alreadyhasconst" func GoconstC() { - a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences, but such constant `AlreadyHasConst` already exists" + a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences as assignment statement, but such constant `AlreadyHasConst` already exists" fmt.Print(a) b := "alreadyhasconst" fmt.Print(b) c := "alreadyhasconst" fmt.Print(c) + fmt.Print("alreadyhasconst") } diff --git a/test/testdata/goconst_calls_enabled.go b/test/testdata/goconst_calls_enabled.go new file mode 100644 index 000000000000..3e300b6b406f --- /dev/null +++ b/test/testdata/goconst_calls_enabled.go @@ -0,0 +1,17 @@ +//args: -Egoconst +//config: linters-settings.goconst.ignore-calls=false +package testdata + +import "fmt" + +const FooBar = "foobar" + +func Baz() { + a := "foobar" // ERROR "string `foobar` has 3 occurrences as assignment statement, but such constant `FooBar` already exists" + fmt.Print(a) + b := "foobar" + fmt.Print(b) + c := "foobar" + fmt.Print(c) + fmt.Print("foobar") // ERROR "string `foobar` has 1 occurrences as call statement, but such constant `FooBar` already exists" +}