Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using upstream goconst #1500

Merged
merged 5 commits into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/commands/run.go
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions pkg/config/config.go
Expand Up @@ -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{}
Expand Down
14 changes: 10 additions & 4 deletions pkg/golinters/goconst.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions test/testdata/goconst.go
Expand Up @@ -28,4 +28,5 @@ func GoconstC() {
fmt.Print(b)
c := "alreadyhasconst"
fmt.Print(c)
fmt.Print("alreadyhasconst")
}
17 changes: 17 additions & 0 deletions 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")
}