Skip to content

build(deps): bump github.com/jgautheron/goconst from 1.7.1 to 1.8.1 #5712

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,14 @@ linters:
ignore-calls: false
# Exclude strings matching the given regular expression.
# Default: ""
ignore-strings: 'foo.+'
ignore-string-values:
- 'foo.+'
# Detects constants with identical values.
# Default: false
find-duplicates: true
# Evaluates of constant expressions like Prefix + "suffix".
# 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.1
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.

17 changes: 15 additions & 2 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1487,9 +1487,12 @@
"type": "boolean",
"default": true
},
"ignore-strings": {
"ignore-string-values": {
"description": "Exclude strings matching the given regular expression",
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"numbers": {
"description": "Search also for duplicated numbers.",
Expand All @@ -1505,6 +1508,16 @@
"description": "Maximum value, only works with `numbers`",
"type": "integer",
"default": 3
},
"find-duplicates": {
"description": "Detects constants with identical values",
"type": "boolean",
"default": false
},
"eval-const-expressions": {
"description": "Evaluates of constant expressions like Prefix + \"suffix\"",
"type": "boolean",
"default": false
}
}
},
Expand Down
21 changes: 13 additions & 8 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,19 @@ 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"`
IgnoreStringValues []string `mapstructure:"ignore-string-values"`
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"`
FindDuplicates bool `mapstructure:"find-duplicates"`
EvalConstExpressions bool `mapstructure:"eval-const-expressions"`

// Deprecated: use IgnoreStringValues instead.
IgnoreStrings string `mapstructure:"ignore-strings"`
}

type GoCriticSettings struct {
Expand Down
11 changes: 9 additions & 2 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,15 @@ func (l *Loader) handleDeprecation() error {
return nil
}

func (*Loader) handleLinterOptionDeprecations() {
// The function is empty but deprecations will happen in the future.
func (l *Loader) handleLinterOptionDeprecations() {
// Deprecated since v2.1.0.
if l.cfg.Linters.Settings.Goconst.IgnoreStrings != "" {
l.log.Warnf("The configuration option `linters.settings.goconst.ignore-strings` is deprecated, " +
"please use `linters.settings.goconst.ignore-string-values`.")

l.cfg.Linters.Settings.Goconst.IgnoreStringValues = append(l.cfg.Linters.Settings.Goconst.IgnoreStringValues,
l.cfg.Linters.Settings.Goconst.IgnoreStrings)
}
}

func (l *Loader) handleEnableOnlyOption() error {
Expand Down
51 changes: 34 additions & 17 deletions pkg/golinters/goconst/goconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ func New(settings *config.GoConstSettings) *goanalysis.Linter {
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}).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: settings.IgnoreStringValues,
MatchWithConstants: settings.MatchWithConstants,
MinStringLength: settings.MinStringLen,
MinOccurrences: settings.MinOccurrencesCount,
ParseNumbers: settings.ParseNumbers,
NumberMin: settings.NumberMin,
NumberMax: settings.NumberMax,
ExcludeTypes: map[goconstAPI.Type]bool{},
FindDuplicates: settings.FindDuplicates,
EvalConstExpressions: settings.EvalConstExpressions,

// Should be managed with `linters.exclusions.rules`.
IgnoreTests: false,
Expand All @@ -70,7 +72,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 @@ -80,17 +82,32 @@ 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)
for i := range lintIssues {
issue := &lintIssues[i]

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

switch {
case issue.OccurrencesCount > 0:
text = fmt.Sprintf("string %s has %d occurrences", internal.FormatCode(issue.Str, nil), issue.OccurrencesCount)

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

case issue.DuplicateConst != "":
text = fmt.Sprintf("This constant is a duplicate of %s at %s",
internal.FormatCode(issue.DuplicateConst, nil),
issue.DuplicatePos.String())

default:
continue
}

res = append(res, goanalysis.NewIssue(&result.Issue{
Pos: i.Pos,
Pos: issue.Pos,
Text: text,
FromLinter: linterName,
}, pass))
Expand Down
25 changes: 25 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_eval_and_find_duplicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//golangcitest:args -Egoconst
//golangcitest:config_path testdata/goconst_eval_and_find_duplicates.yml
package testdata

import "fmt"

const (
envPrefix = "FOO_"
EnvUser = envPrefix + "USER"
EnvPassword = envPrefix + "PASSWORD"
)

const EnvUserFull = "FOO_USER" // want "This constant is a duplicate of `EnvUser` at .*goconst_eval_and_find_duplicates.go:9:16"

const KiB = 1 << 10

func _() {
fmt.Println(envPrefix, EnvUser, EnvPassword, EnvUserFull)

const kilobytes = 1024 // want "This constant is a duplicate of `KiB` at .*goconst_eval_and_find_duplicates.go:15:13"
fmt.Println(kilobytes)

kib := 1024
fmt.Println(kib)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "2"

linters:
settings:
goconst:
find-duplicates: true
eval-const-expressions: true
numbers: true
29 changes: 29 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_eval_const_expressions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//golangcitest:args -Egoconst
//golangcitest:config_path testdata/goconst_eval_const_expressions.yml
package testdata

const (
prefix = "example.com/"
API = prefix + "api"
Web = prefix + "web"
)

const Full = "example.com/api"

func _() {
a0 := "example.com/api" // want "string `example.com/api` has 3 occurrences, but such constant `API` already exists"
a1 := "example.com/api"
a2 := "example.com/api"

_ = a0
_ = a1
_ = a2

b0 := "example.com/web" // want "string `example.com/web` has 3 occurrences, but such constant `Web` already exists"
b1 := "example.com/web"
b2 := "example.com/web"

_ = b0
_ = b1
_ = b2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

linters:
settings:
goconst:
eval-const-expressions: true
29 changes: 29 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_find_duplicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//golangcitest:args -Egoconst
//golangcitest:config_path testdata/goconst_find_duplicates.yml
package testdata

const SingleConst = "single constant"

const (
GroupedConst1 = "grouped constant"
GroupedConst2 = "another grouped"
)

const (
GroupedDuplicateConst1 = "grouped duplicate value"
GroupedDuplicateConst2 = "grouped duplicate value" // want "This constant is a duplicate of `GroupedDuplicateConst1` at .*goconst_find_duplicates.go:13:2"
)

const DuplicateConst1 = "duplicate value"

const DuplicateConst2 = "duplicate value" // want "This constant is a duplicate of `DuplicateConst1` at .*goconst_find_duplicates.go:17:7"

const (
SpecialDuplicateConst1 = "special\nvalue\twith\rchars"
SpecialDuplicateConst2 = "special\nvalue\twith\rchars" // want "This constant is a duplicate of `SpecialDuplicateConst1` at .*goconst_find_duplicates.go:22:2"
)

func _() {
const DuplicateScopedConst1 = "duplicate scoped value"
const DuplicateScopedConst2 = "duplicate scoped value" // want "This constant is a duplicate of `DuplicateScopedConst1` at .*goconst_find_duplicates.go:27:8"
}
6 changes: 6 additions & 0 deletions pkg/golinters/goconst/testdata/goconst_find_duplicates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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
Loading