Skip to content

Bump goconst to v1.8.0 #5707

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,12 @@ linters:
# Exclude strings matching the given regular expression.
# Default: ""
ignore-strings: 'foo.+'
# Look for duplicate consts with matching values.
# Default: false
find-duplicates: true
# Evaluate constant expressions when searching for constants.
# Default: false
eval-const-expressions: true

gocritic:
# Disable all checks.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/gostaticanalysis/forcetypeassert v0.2.0
github.com/gostaticanalysis/nilerr v0.1.1
github.com/hashicorp/go-version v1.7.0
github.com/jgautheron/goconst v1.7.1
github.com/jgautheron/goconst v1.8.0
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jjti/go-spancheck v0.6.4
github.com/julz/importas v0.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

18 changes: 10 additions & 8 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,16 @@ type GocognitSettings struct {
}

type GoConstSettings struct {
IgnoreStrings string `mapstructure:"ignore-strings"`
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"`
IgnoreStrings string `mapstructure:"ignore-strings"`
MatchWithConstants bool `mapstructure:"match-constant"`
MinStringLen int `mapstructure:"min-len"`
MinOccurrencesCount int `mapstructure:"min-occurrences"`
FindDuplicates bool `mapstructure:"find-duplicates"`
EvalConstExpressions bool `mapstructure:"eval-const-expressions"`
ParseNumbers bool `mapstructure:"numbers"`
NumberMin int `mapstructure:"min"`
NumberMax int `mapstructure:"max"`
IgnoreCalls bool `mapstructure:"ignore-calls"`
}

type GoCriticSettings struct {
Expand Down
46 changes: 28 additions & 18 deletions pkg/golinters/goconst/goconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,25 @@ func New(settings *config.GoConstSettings) *goanalysis.Linter {
linterName,
"Finds repeated strings that could be replaced by a constant",
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
nil).
WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).
WithLoadMode(goanalysis.LoadModeTypesInfo)
}

func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanalysis.Issue, error) {
cfg := goconstAPI.Config{
IgnoreStrings: settings.IgnoreStrings,
MatchWithConstants: settings.MatchWithConstants,
MinStringLength: settings.MinStringLen,
MinOccurrences: settings.MinOccurrencesCount,
ParseNumbers: settings.ParseNumbers,
NumberMin: settings.NumberMin,
NumberMax: settings.NumberMax,
ExcludeTypes: map[goconstAPI.Type]bool{},
IgnoreStrings: []string{settings.IgnoreStrings},
MatchWithConstants: settings.MatchWithConstants,
MinStringLength: settings.MinStringLen,
MinOccurrences: settings.MinOccurrencesCount,
FindDuplicates: settings.FindDuplicates,
ParseNumbers: settings.ParseNumbers,
EvalConstExpressions: settings.EvalConstExpressions,
NumberMin: settings.NumberMin,
NumberMax: settings.NumberMax,
ExcludeTypes: map[goconstAPI.Type]bool{},

// Should be managed with `linters.exclusions.rules`.
IgnoreTests: false,
Expand All @@ -70,7 +73,7 @@ func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanal
cfg.ExcludeTypes[goconstAPI.Call] = true
}

lintIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
lintIssues, err := goconstAPI.Run(pass.Files, pass.Fset, pass.TypesInfo, &cfg)
if err != nil {
return nil, err
}
Expand All @@ -81,12 +84,19 @@ func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanal

res := make([]goanalysis.Issue, 0, len(lintIssues))
for _, i := range lintIssues {
text := fmt.Sprintf("string %s has %d occurrences", internal.FormatCode(i.Str, nil), i.OccurrencesCount)
var text string
if i.OccurrencesCount > 0 {
text = fmt.Sprintf("string %s has %d occurrences", internal.FormatCode(i.Str, nil), i.OccurrencesCount)

if i.MatchingConst == "" {
text += ", make it a constant"
} else {
text += fmt.Sprintf(", but such constant %s already exists", internal.FormatCode(i.MatchingConst, nil))
}
}

if i.MatchingConst == "" {
text += ", make it a constant"
} else {
text += fmt.Sprintf(", but such constant %s already exists", internal.FormatCode(i.MatchingConst, nil))
if i.DuplicateConst != "" {
text = fmt.Sprintf("const definition is duplicate of %s at %s", internal.FormatCode(i.DuplicateConst, nil), i.DuplicatePos.String())
}

res = append(res, goanalysis.NewIssue(&result.Issue{
Expand Down
23 changes: 23 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_eval_expressions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//golangcitest:args -Egoconst
//golangcitest:config_path testdata/goconst_eval_expressions.yml
package testdata

const Host = "www.golangci.com"
const LinterPath = Host + "/goconst"

const path = "www.golangci.com/goconst" // want "const definition is duplicate of `LinterPath` at goconst_eval_expressions.go:6:20"

const KiB = 1 << 10

func EvalExpr() {
println(path)

const duplicated = "www.golangci.com/goconst" // want "const definition is duplicate of `LinterPath` at goconst_eval_expressions.go:6:20"
println(duplicated)

var unique = "www.golangci.com/another-linter"
println(unique)

const Kilobytes = 1024 // want "const definition is duplicate of `KiB` at goconst_eval_expressions.go:10:13"
println(Kilobytes)
}
9 changes: 9 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_eval_expressions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"

linters:
settings:
goconst:
find-duplicates: true
eval-const-expressions: true
numbers: true

15 changes: 15 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_find_duplicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//golangcitest:args -Egoconst
//golangcitest:config_path testdata/goconst_find_duplicates.yml
package testdata

const AConst = "test"
const (
AnotherConst = "test" // want "const definition is duplicate of `AConst` at goconst_find_duplicates.go:5:7"
UnrelatedConst = "i'm unrelated"
)

func Bazoo() {
const Duplicated = "test" // want "const definition is duplicate of `AConst` at goconst_find_duplicates.go:5:7"

const NotDuplicated = "i'm not duplicated"
}
7 changes: 7 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_find_duplicates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "2"

linters:
settings:
goconst:
find-duplicates: true

1 change: 1 addition & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {

linter.NewConfig(goconst.New(&cfg.Linters.Settings.Goconst)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
WithURL("https://github.com/jgautheron/goconst"),

linter.NewConfig(gocritic.New(&cfg.Linters.Settings.Gocritic, placeholderReplacer)).
Expand Down