Skip to content
Open
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
10 changes: 9 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
pass_filenames: false
- id: golangci-lint-full
name: golangci-lint-full
description: Fast linters runner for Go. Runs on all files in the repo. Use this hook if you use pre-commit in CI.
description: Fast linters runner for Go. Runs on all files in the module. Use this hook if you use pre-commit in CI.
entry: golangci-lint run --fix
types: [go]
language: golang
require_serial: true
pass_filenames: false
- id: golangci-lint-fmt
name: golangci-lint-fmt
description: Fast linters runner for Go. Formats all files in the repo.
entry: golangci-lint fmt
types: [go]
language: golang
require_serial: true
pass_filenames: false
- id: golangci-lint-config-verify
name: golangci-lint-config-verify
description: Verifies the configuration file
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
2 changes: 1 addition & 1 deletion scripts/gen_github_action_config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
require (
github.com/shurcooL/githubv4 v0.0.0-20240429030203-be2daab69064
github.com/stretchr/testify v1.10.0
golang.org/x/oauth2 v0.28.0
golang.org/x/oauth2 v0.29.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions scripts/gen_github_action_config/go.sum

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