@@ -48,19 +48,21 @@ func New(settings *config.GoConstSettings) *goanalysis.Linter {
48
48
nil ,
49
49
).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
50
50
return resIssues
51
- }).WithLoadMode (goanalysis .LoadModeSyntax )
51
+ }).WithLoadMode (goanalysis .LoadModeTypesInfo )
52
52
}
53
53
54
54
func runGoconst (pass * analysis.Pass , settings * config.GoConstSettings ) ([]goanalysis.Issue , error ) {
55
55
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 ,
64
66
65
67
// Should be managed with `linters.exclusions.rules`.
66
68
IgnoreTests : false ,
@@ -70,7 +72,7 @@ func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanal
70
72
cfg .ExcludeTypes [goconstAPI .Call ] = true
71
73
}
72
74
73
- lintIssues , err := goconstAPI .Run (pass .Files , pass .Fset , & cfg )
75
+ lintIssues , err := goconstAPI .Run (pass .Files , pass .Fset , pass . TypesInfo , & cfg )
74
76
if err != nil {
75
77
return nil , err
76
78
}
@@ -80,17 +82,32 @@ func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanal
80
82
}
81
83
82
84
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 ]
85
87
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
90
107
}
91
108
92
109
res = append (res , goanalysis .NewIssue (& result.Issue {
93
- Pos : i .Pos ,
110
+ Pos : issue .Pos ,
94
111
Text : text ,
95
112
FromLinter : linterName ,
96
113
}, pass ))
0 commit comments