Skip to content

Commit

Permalink
gocritic: set Go version in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored and ldez committed Apr 30, 2023
1 parent c5e00dc commit a894df2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
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)
}
}

0 comments on commit a894df2

Please sign in to comment.