Skip to content

Commit

Permalink
Adding support for multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb committed Nov 13, 2020
1 parent 70f3ff1 commit 0c4289c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/commands/run.go
Expand Up @@ -176,6 +176,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
fs.IntVar(&lsc.Goconst.NumberMax, "goconst.max",
3, "maximum value, only works with goconst.numbers")
hideFlag("goconst.max")
fs.BoolVar(&lsc.Goconst.IgnoreCalls, "goconst.ignore-calls",
true, "Goconst: ignore when constant is not used as function argument")
hideFlag("goconst.ignore-calls")

// (@dixonwille) These flag is only used for testing purposes.
fs.StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -202,6 +202,7 @@ type LintersSettings struct {
ParseNumbers bool `mapstructure:"numbers"`
NumberMin int `mapstructure:"min"`
NumberMax int `mapstructure:"max"`
IgnoreCalls bool `mapstructure:"ignore-calls"`
}
Gomnd struct {
Settings map[string]map[string]interface{}
Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/goconst.go
Expand Up @@ -66,7 +66,10 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.

res := make([]goanalysis.Issue, 0, len(goconstIssues))
for _, i := range goconstIssues {
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount)
if lintCtx.Settings().Goconst.IgnoreCalls && i.Typ == goconstAPI.Call {
continue
}
textBegin := fmt.Sprintf("string %s has %d occurrences as %s statement", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount, i.Typ)
var textEnd string
if i.MatchingConst == "" {
textEnd = ", make it a constant"
Expand Down
5 changes: 3 additions & 2 deletions test/testdata/goconst.go
Expand Up @@ -4,7 +4,7 @@ package testdata
import "fmt"

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

func GoconstC() {
a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences, but such constant `AlreadyHasConst` already exists"
a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences as assignment statement, but such constant `AlreadyHasConst` already exists"
fmt.Print(a)
b := "alreadyhasconst"
fmt.Print(b)
c := "alreadyhasconst"
fmt.Print(c)
fmt.Print("alreadyhasconst")
}
17 changes: 17 additions & 0 deletions test/testdata/goconst_calls_enabled.go
@@ -0,0 +1,17 @@
//args: -Egoconst
//config: linters-settings.goconst.ignore-calls=false
package testdata

import "fmt"

const FooBar = "foobar"

func Baz() {
a := "foobar" // ERROR "string `foobar` has 3 occurrences as assignment statement, but such constant `FooBar` already exists"
fmt.Print(a)
b := "foobar"
fmt.Print(b)
c := "foobar"
fmt.Print(c)
fmt.Print("foobar") // ERROR "string `foobar` has 1 occurrences as call statement, but such constant `FooBar` already exists"
}

0 comments on commit 0c4289c

Please sign in to comment.