Skip to content

Add IgnoreTests to goconst configuration. #2044

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 2 commits into from
Jun 9, 2021
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
github.com/gostaticanalysis/forcetypeassert v0.0.0-20200621232751-01d4955beaa5
github.com/gostaticanalysis/nilerr v0.1.1
github.com/hashicorp/go-multierror v1.1.1
github.com/jgautheron/goconst v1.4.0
github.com/jgautheron/goconst v1.5.1
github.com/jingyugao/rowserrcheck v1.1.0
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d
Expand Down
6 changes: 2 additions & 4 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ type GocognitSettings struct {
}

type GoConstSettings struct {
IgnoreTests bool `mapstructure:"ignore-tests"`
MatchWithConstants bool `mapstructure:"match-constant"`
MinStringLen int `mapstructure:"min-len"`
MinOccurrencesCount int `mapstructure:"min-occurrences"`
Expand Down
1 change: 1 addition & 0 deletions pkg/golinters/goconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewGoconst() *goanalysis.Linter {

func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.Issue, error) {
cfg := goconstAPI.Config{
IgnoreTests: lintCtx.Settings().Goconst.IgnoreTests,
MatchWithConstants: lintCtx.Settings().Goconst.MatchWithConstants,
MinStringLength: lintCtx.Settings().Goconst.MinStringLen,
MinOccurrences: lintCtx.Settings().Goconst.MinOccurrencesCount,
Expand Down
36 changes: 36 additions & 0 deletions test/testdata/goconst_dont_ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//args: -Egoconst
//config: linters-settings.goconst.ignore-tests=false
package testdata

import (
"fmt"
"testing"
)

func TestGoConstA(t *testing.T) {
a := "needconst" // ERROR "string `needconst` has 5 occurrences, make it a constant"
fmt.Print(a)
b := "needconst"
fmt.Print(b)
c := "needconst"
fmt.Print(c)
}

func TestGoConstB(t *testing.T) {
a := "needconst"
fmt.Print(a)
b := "needconst"
fmt.Print(b)
}

const AlreadyHasConst = "alreadyhasconst"

func TestGoConstC(t *testing.T) {
a := "alreadyhasconst" // ERROR "string `alreadyhasconst` has 3 occurrences, but such constant `AlreadyHasConst` already exists"
fmt.Print(a)
b := "alreadyhasconst"
fmt.Print(b)
c := "alreadyhasconst"
fmt.Print(c)
fmt.Print("alreadyhasconst")
}
36 changes: 36 additions & 0 deletions test/testdata/goconst_ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//args: -Egoconst
//config: linters-settings.goconst.ignore-tests=true
package testdata

import (
"fmt"
"testing"
)

func TestGoConstA(t *testing.T) {
a := "needconst"
fmt.Print(a)
b := "needconst"
fmt.Print(b)
c := "needconst"
fmt.Print(c)
}

func TestGoConstB(t *testing.T) {
a := "needconst"
fmt.Print(a)
b := "needconst"
fmt.Print(b)
}

const AlreadyHasConst = "alreadyhasconst"

func TestGoConstC(t *testing.T) {
a := "alreadyhasconst"
fmt.Print(a)
b := "alreadyhasconst"
fmt.Print(b)
c := "alreadyhasconst"
fmt.Print(c)
fmt.Print("alreadyhasconst")
}