From a894df286743fdf50302e8bc196ff23d46ad66f4 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 17 Apr 2023 17:49:45 +0300 Subject: [PATCH] gocritic: set Go version in configuration --- pkg/config/linters_settings.go | 1 + pkg/golinters/gocritic.go | 2 ++ pkg/lint/lintersdb/manager.go | 4 ++++ test/testdata/configs/gocritic.yml | 3 +++ test/testdata/gocritic.go | 11 +++++++++++ 5 files changed, 21 insertions(+) diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 9386b4631e19..0b2327be0c1b 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -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"` diff --git a/pkg/golinters/gocritic.go b/pkg/golinters/gocritic.go index b5b6d99b5684..1319c72d9d42 100644 --- a/pkg/golinters/gocritic.go +++ b/pkg/golinters/gocritic.go @@ -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 diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index ffe10721cf43..948bda227b4b 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -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 } diff --git a/test/testdata/configs/gocritic.yml b/test/testdata/configs/gocritic.yml index 019e6afb83fa..e7796e6c48ba 100644 --- a/test/testdata/configs/gocritic.yml +++ b/test/testdata/configs/gocritic.yml @@ -1,3 +1,5 @@ +run: + go: 1.14 linters-settings: gocritic: enabled-checks: @@ -5,6 +7,7 @@ linters-settings: - flagDeref - wrapperFunc - ruleguard + - syncMapLoadAndDelete # ignored because sync#Map.LoadAndDelete added in Go 1.15 settings: rangeValCopy: sizeThreshold: 2 diff --git a/test/testdata/gocritic.go b/test/testdata/gocritic.go index afca6f3c479c..93574c1678b7 100644 --- a/test/testdata/gocritic.go +++ b/test/testdata/gocritic.go @@ -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` @@ -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) + } +}