Skip to content
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

gocritic: set Go version in configuration #3807

Closed
Closed
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 @@ -32,7 +32,7 @@ require (
github.com/fatih/color v1.15.0
github.com/firefart/nonamedreturns v1.0.4
github.com/fzipp/gocyclo v0.6.0
github.com/go-critic/go-critic v0.7.0
github.com/go-critic/go-critic v0.8.0
github.com/go-xmlfmt/xmlfmt v1.1.2
github.com/gofrs/flock v0.8.1
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
Expand Down
4 changes: 2 additions & 2 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 @@ -371,6 +371,7 @@ type GoConstSettings struct {
}

type GoCriticSettings struct {
Go string `mapstructure:"-"`
EnabledChecks []string `mapstructure:"enabled-checks"`
DisabledChecks []string `mapstructure:"disabled-checks"`
EnabledTags []string `mapstructure:"enabled-tags"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/golinters/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func (w *goCriticWrapper) run(pass *analysis.Pass) ([]goanalysis.Issue, error) {

linterCtx := gocriticlinter.NewContext(pass.Fset, w.sizes)

linterCtx.SetGoVersion(w.settingsWrapper.Go)

enabledCheckers, err := w.buildEnabledCheckers(linterCtx)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
govetCfg.Go = m.cfg.Run.Go
}

if gocriticCfg != nil {
gocriticCfg.Go = m.cfg.Run.Go
}

if gofumptCfg != nil && gofumptCfg.LangVersion == "" {
gofumptCfg.LangVersion = m.cfg.Run.Go
}
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/gocritic.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
run:
go: 1.14
linters-settings:
gocritic:
enabled-checks:
- rangeValCopy
- flagDeref
- wrapperFunc
- ruleguard
- syncMapLoadAndDelete # ignored because sync#Map.LoadAndDelete added in Go 1.15
settings:
rangeValCopy:
sizeThreshold: 2
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"log"
"strings"
"sync"
)

var _ = *flag.Bool("global1", false, "") // want `flagDeref: immediate deref in \*flag.Bool\(.global1., false, ..\) is most likely an error; consider using flag\.BoolVar`
Expand Down Expand Up @@ -46,3 +47,13 @@ func gocriticDup(x bool) {
func gocriticRuleWrapperFunc() {
strings.Replace("abcabc", "a", "d", -1) // want "ruleguard: this Replace call can be simplified.*"
}

func gocriticSink(args ...any) {}

func gocriticIgnoreSyncMapLoadAndDelete(cond bool, m, m2 *sync.Map) {
actual, ok := m.Load("key")
if ok {
m.Delete("key")
gocriticSink(actual)
}
}