Skip to content

Commit 0c4289c

Browse files
committed
Adding support for multiple types
1 parent 70f3ff1 commit 0c4289c

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

pkg/commands/run.go

+3
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
176176
fs.IntVar(&lsc.Goconst.NumberMax, "goconst.max",
177177
3, "maximum value, only works with goconst.numbers")
178178
hideFlag("goconst.max")
179+
fs.BoolVar(&lsc.Goconst.IgnoreCalls, "goconst.ignore-calls",
180+
true, "Goconst: ignore when constant is not used as function argument")
181+
hideFlag("goconst.ignore-calls")
179182

180183
// (@dixonwille) These flag is only used for testing purposes.
181184
fs.StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ type LintersSettings struct {
202202
ParseNumbers bool `mapstructure:"numbers"`
203203
NumberMin int `mapstructure:"min"`
204204
NumberMax int `mapstructure:"max"`
205+
IgnoreCalls bool `mapstructure:"ignore-calls"`
205206
}
206207
Gomnd struct {
207208
Settings map[string]map[string]interface{}

pkg/golinters/goconst.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.
6666

6767
res := make([]goanalysis.Issue, 0, len(goconstIssues))
6868
for _, i := range goconstIssues {
69-
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount)
69+
if lintCtx.Settings().Goconst.IgnoreCalls && i.Typ == goconstAPI.Call {
70+
continue
71+
}
72+
textBegin := fmt.Sprintf("string %s has %d occurrences as %s statement", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount, i.Typ)
7073
var textEnd string
7174
if i.MatchingConst == "" {
7275
textEnd = ", make it a constant"

test/testdata/goconst.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package testdata
44
import "fmt"
55

66
func GoconstA() {
7-
a := "needconst" // ERROR "string `needconst` has 5 occurrences, make it a constant"
7+
a := "needconst" // ERROR "string `needconst` has 5 occurrences as assignment statement, make it a constant"
88
fmt.Print(a)
99
b := "needconst"
1010
fmt.Print(b)
@@ -22,10 +22,11 @@ func GoconstB() {
2222
const AlreadyHasConst = "alreadyhasconst"
2323

2424
func GoconstC() {
25-
a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences, but such constant `AlreadyHasConst` already exists"
25+
a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences as assignment statement, but such constant `AlreadyHasConst` already exists"
2626
fmt.Print(a)
2727
b := "alreadyhasconst"
2828
fmt.Print(b)
2929
c := "alreadyhasconst"
3030
fmt.Print(c)
31+
fmt.Print("alreadyhasconst")
3132
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//args: -Egoconst
2+
//config: linters-settings.goconst.ignore-calls=false
3+
package testdata
4+
5+
import "fmt"
6+
7+
const FooBar = "foobar"
8+
9+
func Baz() {
10+
a := "foobar" // ERROR "string `foobar` has 3 occurrences as assignment statement, but such constant `FooBar` already exists"
11+
fmt.Print(a)
12+
b := "foobar"
13+
fmt.Print(b)
14+
c := "foobar"
15+
fmt.Print(c)
16+
fmt.Print("foobar") // ERROR "string `foobar` has 1 occurrences as call statement, but such constant `FooBar` already exists"
17+
}

0 commit comments

Comments
 (0)