Skip to content

Commit f4abafb

Browse files
committed
Using upstrem goconst
1 parent 3b06269 commit f4abafb

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
1818
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
1919
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613
20-
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3
2120
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d
2221
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
2322
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc
@@ -27,6 +26,7 @@ require (
2726
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
2827
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
2928
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
29+
github.com/jgautheron/goconst v0.0.0-20201108141142-b58d7cf68fc6
3030
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
3131
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
3232
github.com/kyoh86/exportloopref v0.1.7

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/run.go

+12
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,24 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
158158
150, "Dupl: Minimal threshold to detect copy-paste")
159159
hideFlag("dupl.threshold")
160160

161+
fs.BoolVar(&lsc.Goconst.MatchWithConstants, "goconst.match-constant",
162+
true, "Goconst: look for existing constants matching the values")
163+
hideFlag("goconst.match-constant")
161164
fs.IntVar(&lsc.Goconst.MinStringLen, "goconst.min-len",
162165
3, "Goconst: minimum constant string length")
163166
hideFlag("goconst.min-len")
164167
fs.IntVar(&lsc.Goconst.MinOccurrencesCount, "goconst.min-occurrences",
165168
3, "Goconst: minimum occurrences of constant string count to trigger issue")
166169
hideFlag("goconst.min-occurrences")
170+
fs.BoolVar(&lsc.Goconst.ParseNumbers, "goconst.numbers",
171+
true, "Goconst: search also for duplicated numbers")
172+
hideFlag("goconst.numbers")
173+
fs.IntVar(&lsc.Goconst.NumberMin, "goconst.min",
174+
3, "minimum value, only works with goconst.numbers")
175+
hideFlag("goconst.min")
176+
fs.IntVar(&lsc.Goconst.NumberMin, "goconst.max",
177+
3, "maximum value, only works with goconst.numbers")
178+
hideFlag("goconst.max")
167179

168180
// (@dixonwille) These flag is only used for testing purposes.
169181
fs.StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,

pkg/config/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ type LintersSettings struct {
185185
Threshold int
186186
}
187187
Goconst struct {
188-
MinStringLen int `mapstructure:"min-len"`
189-
MinOccurrencesCount int `mapstructure:"min-occurrences"`
188+
MatchWithConstants bool `mapstructure:"match-constant"`
189+
MinStringLen int `mapstructure:"min-len"`
190+
MinOccurrencesCount int `mapstructure:"min-occurrences"`
191+
ParseNumbers bool `mapstructure:"numbers"`
192+
NumberMin int `mapstructure:"min"`
193+
NumberMax int `mapstructure:"max"`
190194
}
191195
Gomnd struct {
192196
Settings map[string]map[string]interface{}

pkg/golinters/goconst.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"sync"
66

7-
goconstAPI "github.com/golangci/goconst"
7+
goconstAPI "github.com/jgautheron/goconst"
88
"golang.org/x/tools/go/analysis"
99

1010
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
@@ -47,9 +47,12 @@ func NewGoconst() *goanalysis.Linter {
4747

4848
func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.Issue, error) {
4949
cfg := goconstAPI.Config{
50-
MatchWithConstants: true,
50+
MatchWithConstants: lintCtx.Settings().Goconst.MatchWithConstants,
5151
MinStringLength: lintCtx.Settings().Goconst.MinStringLen,
5252
MinOccurrences: lintCtx.Settings().Goconst.MinOccurrencesCount,
53+
ParseNumbers: lintCtx.Settings().Goconst.ParseNumbers,
54+
NumberMin: lintCtx.Settings().Goconst.NumberMin,
55+
NumberMax: lintCtx.Settings().Goconst.NumberMax,
5356
}
5457

5558
goconstIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
@@ -63,7 +66,7 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.
6366

6467
res := make([]goanalysis.Issue, 0, len(goconstIssues))
6568
for _, i := range goconstIssues {
66-
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurencesCount)
69+
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount)
6770
var textEnd string
6871
if i.MatchingConst == "" {
6972
textEnd = ", make it a constant"

0 commit comments

Comments
 (0)