Skip to content

Commit 72fe3b6

Browse files
build(deps): bump github.com/jgautheron/goconst from 1.7.1 to 1.8.1 (#5712)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 927cad8 commit 72fe3b6

14 files changed

+186
-33
lines changed

Diff for: .golangci.next.reference.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,14 @@ linters:
631631
ignore-calls: false
632632
# Exclude strings matching the given regular expression.
633633
# Default: ""
634-
ignore-strings: 'foo.+'
634+
ignore-string-values:
635+
- 'foo.+'
636+
# Detects constants with identical values.
637+
# Default: false
638+
find-duplicates: true
639+
# Evaluates of constant expressions like Prefix + "suffix".
640+
# Default: false
641+
eval-const-expressions: true
635642

636643
gocritic:
637644
# Disable all checks.

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ require (
5757
github.com/gostaticanalysis/forcetypeassert v0.2.0
5858
github.com/gostaticanalysis/nilerr v0.1.1
5959
github.com/hashicorp/go-version v1.7.0
60-
github.com/jgautheron/goconst v1.7.1
60+
github.com/jgautheron/goconst v1.8.1
6161
github.com/jingyugao/rowserrcheck v1.1.1
6262
github.com/jjti/go-spancheck v0.6.4
6363
github.com/julz/importas v0.2.0

Diff for: go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: jsonschema/golangci.next.jsonschema.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,12 @@
14871487
"type": "boolean",
14881488
"default": true
14891489
},
1490-
"ignore-strings": {
1490+
"ignore-string-values": {
14911491
"description": "Exclude strings matching the given regular expression",
1492-
"type": "string"
1492+
"type": "array",
1493+
"items": {
1494+
"type": "string"
1495+
}
14931496
},
14941497
"numbers": {
14951498
"description": "Search also for duplicated numbers.",
@@ -1505,6 +1508,16 @@
15051508
"description": "Maximum value, only works with `numbers`",
15061509
"type": "integer",
15071510
"default": 3
1511+
},
1512+
"find-duplicates": {
1513+
"description": "Detects constants with identical values",
1514+
"type": "boolean",
1515+
"default": false
1516+
},
1517+
"eval-const-expressions": {
1518+
"description": "Evaluates of constant expressions like Prefix + \"suffix\"",
1519+
"type": "boolean",
1520+
"default": false
15081521
}
15091522
}
15101523
},

Diff for: pkg/config/linters_settings.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,19 @@ type GocognitSettings struct {
451451
}
452452

453453
type GoConstSettings struct {
454-
IgnoreStrings string `mapstructure:"ignore-strings"`
455-
MatchWithConstants bool `mapstructure:"match-constant"`
456-
MinStringLen int `mapstructure:"min-len"`
457-
MinOccurrencesCount int `mapstructure:"min-occurrences"`
458-
ParseNumbers bool `mapstructure:"numbers"`
459-
NumberMin int `mapstructure:"min"`
460-
NumberMax int `mapstructure:"max"`
461-
IgnoreCalls bool `mapstructure:"ignore-calls"`
454+
IgnoreStringValues []string `mapstructure:"ignore-string-values"`
455+
MatchWithConstants bool `mapstructure:"match-constant"`
456+
MinStringLen int `mapstructure:"min-len"`
457+
MinOccurrencesCount int `mapstructure:"min-occurrences"`
458+
ParseNumbers bool `mapstructure:"numbers"`
459+
NumberMin int `mapstructure:"min"`
460+
NumberMax int `mapstructure:"max"`
461+
IgnoreCalls bool `mapstructure:"ignore-calls"`
462+
FindDuplicates bool `mapstructure:"find-duplicates"`
463+
EvalConstExpressions bool `mapstructure:"eval-const-expressions"`
464+
465+
// Deprecated: use IgnoreStringValues instead.
466+
IgnoreStrings string `mapstructure:"ignore-strings"`
462467
}
463468

464469
type GoCriticSettings struct {

Diff for: pkg/config/loader.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,15 @@ func (l *Loader) handleDeprecation() error {
190190
return nil
191191
}
192192

193-
func (*Loader) handleLinterOptionDeprecations() {
194-
// The function is empty but deprecations will happen in the future.
193+
func (l *Loader) handleLinterOptionDeprecations() {
194+
// Deprecated since v2.1.0.
195+
if l.cfg.Linters.Settings.Goconst.IgnoreStrings != "" {
196+
l.log.Warnf("The configuration option `linters.settings.goconst.ignore-strings` is deprecated, " +
197+
"please use `linters.settings.goconst.ignore-string-values`.")
198+
199+
l.cfg.Linters.Settings.Goconst.IgnoreStringValues = append(l.cfg.Linters.Settings.Goconst.IgnoreStringValues,
200+
l.cfg.Linters.Settings.Goconst.IgnoreStrings)
201+
}
195202
}
196203

197204
func (l *Loader) handleEnableOnlyOption() error {

Diff for: pkg/golinters/goconst/goconst.go

+34-17
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,21 @@ func New(settings *config.GoConstSettings) *goanalysis.Linter {
4848
nil,
4949
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
5050
return resIssues
51-
}).WithLoadMode(goanalysis.LoadModeSyntax)
51+
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
5252
}
5353

5454
func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanalysis.Issue, error) {
5555
cfg := goconstAPI.Config{
56-
IgnoreStrings: settings.IgnoreStrings,
57-
MatchWithConstants: settings.MatchWithConstants,
58-
MinStringLength: settings.MinStringLen,
59-
MinOccurrences: settings.MinOccurrencesCount,
60-
ParseNumbers: settings.ParseNumbers,
61-
NumberMin: settings.NumberMin,
62-
NumberMax: settings.NumberMax,
63-
ExcludeTypes: map[goconstAPI.Type]bool{},
56+
IgnoreStrings: settings.IgnoreStringValues,
57+
MatchWithConstants: settings.MatchWithConstants,
58+
MinStringLength: settings.MinStringLen,
59+
MinOccurrences: settings.MinOccurrencesCount,
60+
ParseNumbers: settings.ParseNumbers,
61+
NumberMin: settings.NumberMin,
62+
NumberMax: settings.NumberMax,
63+
ExcludeTypes: map[goconstAPI.Type]bool{},
64+
FindDuplicates: settings.FindDuplicates,
65+
EvalConstExpressions: settings.EvalConstExpressions,
6466

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

73-
lintIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
75+
lintIssues, err := goconstAPI.Run(pass.Files, pass.Fset, pass.TypesInfo, &cfg)
7476
if err != nil {
7577
return nil, err
7678
}
@@ -80,17 +82,32 @@ func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanal
8082
}
8183

8284
res := make([]goanalysis.Issue, 0, len(lintIssues))
83-
for _, i := range lintIssues {
84-
text := fmt.Sprintf("string %s has %d occurrences", internal.FormatCode(i.Str, nil), i.OccurrencesCount)
85+
for i := range lintIssues {
86+
issue := &lintIssues[i]
8587

86-
if i.MatchingConst == "" {
87-
text += ", make it a constant"
88-
} else {
89-
text += fmt.Sprintf(", but such constant %s already exists", internal.FormatCode(i.MatchingConst, nil))
88+
var text string
89+
90+
switch {
91+
case issue.OccurrencesCount > 0:
92+
text = fmt.Sprintf("string %s has %d occurrences", internal.FormatCode(issue.Str, nil), issue.OccurrencesCount)
93+
94+
if issue.MatchingConst == "" {
95+
text += ", make it a constant"
96+
} else {
97+
text += fmt.Sprintf(", but such constant %s already exists", internal.FormatCode(issue.MatchingConst, nil))
98+
}
99+
100+
case issue.DuplicateConst != "":
101+
text = fmt.Sprintf("This constant is a duplicate of %s at %s",
102+
internal.FormatCode(issue.DuplicateConst, nil),
103+
issue.DuplicatePos.String())
104+
105+
default:
106+
continue
90107
}
91108

92109
res = append(res, goanalysis.NewIssue(&result.Issue{
93-
Pos: i.Pos,
110+
Pos: issue.Pos,
94111
Text: text,
95112
FromLinter: linterName,
96113
}, pass))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//golangcitest:args -Egoconst
2+
//golangcitest:config_path testdata/goconst_eval_and_find_duplicates.yml
3+
package testdata
4+
5+
import "fmt"
6+
7+
const (
8+
envPrefix = "FOO_"
9+
EnvUser = envPrefix + "USER"
10+
EnvPassword = envPrefix + "PASSWORD"
11+
)
12+
13+
const EnvUserFull = "FOO_USER" // want "This constant is a duplicate of `EnvUser` at .*goconst_eval_and_find_duplicates.go:9:16"
14+
15+
const KiB = 1 << 10
16+
17+
func _() {
18+
fmt.Println(envPrefix, EnvUser, EnvPassword, EnvUserFull)
19+
20+
const kilobytes = 1024 // want "This constant is a duplicate of `KiB` at .*goconst_eval_and_find_duplicates.go:15:13"
21+
fmt.Println(kilobytes)
22+
23+
kib := 1024
24+
fmt.Println(kib)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
goconst:
6+
find-duplicates: true
7+
eval-const-expressions: true
8+
numbers: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//golangcitest:args -Egoconst
2+
//golangcitest:config_path testdata/goconst_eval_const_expressions.yml
3+
package testdata
4+
5+
const (
6+
prefix = "example.com/"
7+
API = prefix + "api"
8+
Web = prefix + "web"
9+
)
10+
11+
const Full = "example.com/api"
12+
13+
func _() {
14+
a0 := "example.com/api" // want "string `example.com/api` has 3 occurrences, but such constant `API` already exists"
15+
a1 := "example.com/api"
16+
a2 := "example.com/api"
17+
18+
_ = a0
19+
_ = a1
20+
_ = a2
21+
22+
b0 := "example.com/web" // want "string `example.com/web` has 3 occurrences, but such constant `Web` already exists"
23+
b1 := "example.com/web"
24+
b2 := "example.com/web"
25+
26+
_ = b0
27+
_ = b1
28+
_ = b2
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
goconst:
6+
eval-const-expressions: true
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//golangcitest:args -Egoconst
2+
//golangcitest:config_path testdata/goconst_find_duplicates.yml
3+
package testdata
4+
5+
const SingleConst = "single constant"
6+
7+
const (
8+
GroupedConst1 = "grouped constant"
9+
GroupedConst2 = "another grouped"
10+
)
11+
12+
const (
13+
GroupedDuplicateConst1 = "grouped duplicate value"
14+
GroupedDuplicateConst2 = "grouped duplicate value" // want "This constant is a duplicate of `GroupedDuplicateConst1` at .*goconst_find_duplicates.go:13:2"
15+
)
16+
17+
const DuplicateConst1 = "duplicate value"
18+
19+
const DuplicateConst2 = "duplicate value" // want "This constant is a duplicate of `DuplicateConst1` at .*goconst_find_duplicates.go:17:7"
20+
21+
const (
22+
SpecialDuplicateConst1 = "special\nvalue\twith\rchars"
23+
SpecialDuplicateConst2 = "special\nvalue\twith\rchars" // want "This constant is a duplicate of `SpecialDuplicateConst1` at .*goconst_find_duplicates.go:22:2"
24+
)
25+
26+
func _() {
27+
const DuplicateScopedConst1 = "duplicate scoped value"
28+
const DuplicateScopedConst2 = "duplicate scoped value" // want "This constant is a duplicate of `DuplicateScopedConst1` at .*goconst_find_duplicates.go:27:8"
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
goconst:
6+
find-duplicates: true

Diff for: pkg/lint/lintersdb/builder_linter.go

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
300300

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

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

0 commit comments

Comments
 (0)