Skip to content

Commit

Permalink
dev: refactor govet impl with slices.Contains (#4372)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <[email protected]>
  • Loading branch information
alexandear and ldez authored Feb 9, 2024
1 parent 6f26840 commit 7cf6cc9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
39 changes: 13 additions & 26 deletions pkg/golinters/govet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package golinters

import (
"slices"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/appends"
"golang.org/x/tools/go/analysis/passes/asmdecl"
Expand Down Expand Up @@ -170,40 +172,25 @@ func analyzersFromConfig(settings *config.GovetSettings) []*analysis.Analyzer {
}

func isAnalyzerEnabled(name string, cfg *config.GovetSettings, defaultAnalyzers []*analysis.Analyzer) bool {
// TODO(ldez) remove loopclosure when go1.23
if name == loopclosure.Analyzer.Name && config.IsGreaterThanOrEqualGo122(cfg.Go) {
return false
}

if cfg.EnableAll {
for _, n := range cfg.Disable {
if n == name {
return false
}
}
return true
}
switch {
case cfg.EnableAll:
return !slices.Contains(cfg.Disable, name)

// Raw for loops should be OK on small slice lengths.
for _, n := range cfg.Enable {
if n == name {
return true
}
}
case slices.Contains(cfg.Enable, name):
return true

for _, n := range cfg.Disable {
if n == name {
return false
}
}
case slices.Contains(cfg.Disable, name):
return false

if cfg.DisableAll {
case cfg.DisableAll:
return false
}

for _, a := range defaultAnalyzers {
if a.Name == name {
return true
}
default:
return slices.ContainsFunc(defaultAnalyzers, func(a *analysis.Analyzer) bool { return a.Name == name })
}
return false
}
3 changes: 3 additions & 0 deletions pkg/golinters/govet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestGovetAnalyzerIsEnabled(t *testing.T) {
Disable []string
EnableAll bool
DisableAll bool
Go string

Name string
Enabled bool
Expand All @@ -83,12 +84,14 @@ func TestGovetAnalyzerIsEnabled(t *testing.T) {
{Name: "unsafeptr", Enabled: true, Enable: []string{"unsafeptr"}},
{Name: "shift", Enabled: true, EnableAll: true},
{Name: "shadow", EnableAll: true, Disable: []string{"shadow"}, Enabled: false},
{Name: "loopclosure", EnableAll: true, Enabled: false, Go: "1.22"}, // TODO(ldez) remove loopclosure when go1.23
} {
cfg := &config.GovetSettings{
Enable: tc.Enable,
Disable: tc.Disable,
EnableAll: tc.EnableAll,
DisableAll: tc.DisableAll,
Go: tc.Go,
}
if enabled := isAnalyzerEnabled(tc.Name, cfg, defaultAnalyzers); enabled != tc.Enabled {
t.Errorf("%+v", tc)
Expand Down

0 comments on commit 7cf6cc9

Please sign in to comment.