diff --git a/go.mod b/go.mod index 288c0cabc37c..842b71a68a92 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,6 @@ require ( github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 - github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc @@ -27,6 +26,7 @@ require ( github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0 github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 + github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3 github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3 github.com/kyoh86/exportloopref v0.1.8 diff --git a/go.sum b/go.sum index a8353c6caf54..0bf2a164e277 100644 --- a/go.sum +++ b/go.sum @@ -56,7 +56,6 @@ github.com/denis-tingajkin/go-header v0.3.1/go.mod h1:sq/2IxMhaZX+RRcgHfCRx/m0M5 github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= @@ -124,8 +123,6 @@ github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 h1:YYWNAGTKWhKpc github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6/go.mod h1:DbHgvLiFKX1Sh2T1w8Q/h4NAI8MHIpzCdnBUDTXU3I0= github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 h1:9kfjN3AdxcbsZBf8NjltjWihK2QfBBBZuv91cMFfDHw= github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613/go.mod h1:SyvUF2NxV+sN8upjjeVYr5W7tyxaT1JVtvhKhOn2ii8= -github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 h1:pe9JHs3cHHDQgOFXJJdYkK6fLz2PWyYtP4hthoCMvs8= -github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3/go.mod h1:JXrF4TWy4tXYn62/9x8Wm/K/dm06p8tCKwFRDPZG/1o= github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d h1:pXTK/gkVNs7Zyy7WKgLXmpQ5bHTrq5GDsp8R9Qs67g0= github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d/go.mod h1:ozx7R9SIwqmqf5pRP90DhR2Oay2UIjGuKheCBCNwAYU= github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a h1:iR3fYXUjHCR97qWS8ch1y9zPNsgXThGwjKPrYfqMPks= @@ -197,6 +194,8 @@ github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/J github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3 h1:7nkB9fLPMwtn/R6qfPcHileL/x9ydlhw8XyDrLI1ZXg= +github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4= github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a h1:GmsqmapfzSJkm28dhRoHz2tLRbJmqhU86IPgBtN3mmk= github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a/go.mod h1:xRskid8CManxVta/ALEhJha/pweKBaVG6fWgc0yH25s= github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3 h1:jNYPNLe3d8smommaoQlK7LOA5ESyUJJ+Wf79ZtA7Vp4= @@ -225,6 +224,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= @@ -283,7 +283,6 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.14.1 h1:jMU0WaQrP0a/YAEq8eJmJKjBoMs+pClEr1vDMlM/Do4= github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= -github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs= github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 57cb5471f5dc..15cd670e11e6 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -158,12 +158,27 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is 150, "Dupl: Minimal threshold to detect copy-paste") hideFlag("dupl.threshold") + fs.BoolVar(&lsc.Goconst.MatchWithConstants, "goconst.match-constant", + true, "Goconst: look for existing constants matching the values") + hideFlag("goconst.match-constant") fs.IntVar(&lsc.Goconst.MinStringLen, "goconst.min-len", 3, "Goconst: minimum constant string length") hideFlag("goconst.min-len") fs.IntVar(&lsc.Goconst.MinOccurrencesCount, "goconst.min-occurrences", 3, "Goconst: minimum occurrences of constant string count to trigger issue") hideFlag("goconst.min-occurrences") + fs.BoolVar(&lsc.Goconst.ParseNumbers, "goconst.numbers", + false, "Goconst: search also for duplicated numbers") + hideFlag("goconst.numbers") + fs.IntVar(&lsc.Goconst.NumberMin, "goconst.min", + 3, "minimum value, only works with goconst.numbers") + hideFlag("goconst.min") + 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 cf3a3d59bba2..4ec378f7a687 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -196,8 +196,13 @@ type LintersSettings struct { Threshold int } Goconst struct { - MinStringLen int `mapstructure:"min-len"` - MinOccurrencesCount int `mapstructure:"min-occurrences"` + MatchWithConstants bool `mapstructure:"match-constant"` + MinStringLen int `mapstructure:"min-len"` + MinOccurrencesCount int `mapstructure:"min-occurrences"` + 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 8e91feef8942..edda310d4067 100644 --- a/pkg/golinters/goconst.go +++ b/pkg/golinters/goconst.go @@ -4,7 +4,7 @@ import ( "fmt" "sync" - goconstAPI "github.com/golangci/goconst" + goconstAPI "github.com/jgautheron/goconst" "golang.org/x/tools/go/analysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" @@ -47,11 +47,17 @@ func NewGoconst() *goanalysis.Linter { func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.Issue, error) { cfg := goconstAPI.Config{ - MatchWithConstants: true, + MatchWithConstants: lintCtx.Settings().Goconst.MatchWithConstants, MinStringLength: lintCtx.Settings().Goconst.MinStringLen, MinOccurrences: lintCtx.Settings().Goconst.MinOccurrencesCount, + ParseNumbers: lintCtx.Settings().Goconst.ParseNumbers, + NumberMin: lintCtx.Settings().Goconst.NumberMin, + NumberMax: lintCtx.Settings().Goconst.NumberMax, + ExcludeTypes: map[goconstAPI.Type]bool{}, + } + if lintCtx.Settings().Goconst.IgnoreCalls { + cfg.ExcludeTypes[goconstAPI.Call] = true } - goconstIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg) if err != nil { return nil, err @@ -63,7 +69,7 @@ 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.OccurencesCount) + textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount) 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..5942c5667201 100644 --- a/test/testdata/goconst.go +++ b/test/testdata/goconst.go @@ -28,4 +28,5 @@ func GoconstC() { 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..dd1a0babb43a --- /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 4 occurrences, but such constant `FooBar` already exists" + fmt.Print(a) + b := "foobar" + fmt.Print(b) + c := "foobar" + fmt.Print(c) + fmt.Print("foobar") +}