Skip to content

Commit 993337b

Browse files
authored
Using upstrem goconst (#1500)
github.com/golangci/goconst is now obsolete :)
1 parent 9948153 commit 993337b

File tree

7 files changed

+54
-11
lines changed

7 files changed

+54
-11
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
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-20201117150253-ccae5bf973f3
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.8

go.sum

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/run.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,27 @@ 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+
false, "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.NumberMax, "goconst.max",
177+
3, "maximum value, only works with goconst.numbers")
178+
hideFlag("goconst.max")
179+
fs.BoolVar(&lsc.Goconst.IgnoreCalls, "goconst.ignore-calls",
180+
true, "Goconst: ignore when constant is not used as function argument")
181+
hideFlag("goconst.ignore-calls")
167182

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

pkg/config/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,13 @@ type LintersSettings struct {
196196
Threshold int
197197
}
198198
Goconst struct {
199-
MinStringLen int `mapstructure:"min-len"`
200-
MinOccurrencesCount int `mapstructure:"min-occurrences"`
199+
MatchWithConstants bool `mapstructure:"match-constant"`
200+
MinStringLen int `mapstructure:"min-len"`
201+
MinOccurrencesCount int `mapstructure:"min-occurrences"`
202+
ParseNumbers bool `mapstructure:"numbers"`
203+
NumberMin int `mapstructure:"min"`
204+
NumberMax int `mapstructure:"max"`
205+
IgnoreCalls bool `mapstructure:"ignore-calls"`
201206
}
202207
Gomnd struct {
203208
Settings map[string]map[string]interface{}

pkg/golinters/goconst.go

Lines changed: 10 additions & 4 deletions
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,11 +47,17 @@ 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,
56+
ExcludeTypes: map[goconstAPI.Type]bool{},
57+
}
58+
if lintCtx.Settings().Goconst.IgnoreCalls {
59+
cfg.ExcludeTypes[goconstAPI.Call] = true
5360
}
54-
5561
goconstIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
5662
if err != nil {
5763
return nil, err
@@ -63,7 +69,7 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.
6369

6470
res := make([]goanalysis.Issue, 0, len(goconstIssues))
6571
for _, i := range goconstIssues {
66-
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurencesCount)
72+
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount)
6773
var textEnd string
6874
if i.MatchingConst == "" {
6975
textEnd = ", make it a constant"

test/testdata/goconst.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ func GoconstC() {
2828
fmt.Print(b)
2929
c := "alreadyhasconst"
3030
fmt.Print(c)
31+
fmt.Print("alreadyhasconst")
3132
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//args: -Egoconst
2+
//config: linters-settings.goconst.ignore-calls=false
3+
package testdata
4+
5+
import "fmt"
6+
7+
const FooBar = "foobar"
8+
9+
func Baz() {
10+
a := "foobar" // ERROR "string `foobar` has 4 occurrences, but such constant `FooBar` already exists"
11+
fmt.Print(a)
12+
b := "foobar"
13+
fmt.Print(b)
14+
c := "foobar"
15+
fmt.Print(c)
16+
fmt.Print("foobar")
17+
}

0 commit comments

Comments
 (0)