diff --git a/go.mod b/go.mod index c8d5d789504e..dc8076f290e9 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-20201108141142-b58d7cf68fc6 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.7 diff --git a/go.sum b/go.sum index 92699332d89f..ba410a38f95d 100644 --- a/go.sum +++ b/go.sum @@ -196,6 +196,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-20201108141142-b58d7cf68fc6 h1:Q1jWnSiUwwb54MNiYngLbMW5/RBYGkUd6hOGZhAXsOg= +github.com/jgautheron/goconst v0.0.0-20201108141142-b58d7cf68fc6/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= diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 57cb5471f5dc..504c1407b2b4 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -158,12 +158,24 @@ 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", + true, "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.NumberMin, "goconst.max", + 3, "maximum value, only works with goconst.numbers") + hideFlag("goconst.max") // (@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 5b73a36de349..d7fdc09dd46b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -185,8 +185,12 @@ 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"` } Gomnd struct { Settings map[string]map[string]interface{} diff --git a/pkg/golinters/goconst.go b/pkg/golinters/goconst.go index 8e91feef8942..39e967055da4 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,9 +47,12 @@ 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, } goconstIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg) @@ -63,7 +66,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"