Skip to content

Commit 9450c8e

Browse files
committed
feat: disable unsupported go1.18 govet analyzers
1 parent 6963a2e commit 9450c8e

File tree

5 files changed

+65
-58
lines changed

5 files changed

+65
-58
lines changed

pkg/config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ type Version struct {
3939
Format string `mapstructure:"format"`
4040
}
4141

42+
func IsGreaterThanOrEqualGo118(v string) bool {
43+
v1, err := hcversion.NewVersion(strings.TrimPrefix(v, "go"))
44+
if err != nil {
45+
return false
46+
}
47+
48+
limit, err := hcversion.NewVersion("1.18")
49+
if err != nil {
50+
return false
51+
}
52+
53+
return v1.GreaterThanOrEqual(limit)
54+
}
55+
4256
func DetectGo() string {
4357
const defaultGo = "1.17"
4458

pkg/config/linters_settings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ type GoSecSettings struct {
374374
}
375375

376376
type GovetSettings struct {
377-
CheckShadowing bool `mapstructure:"check-shadowing"`
377+
Go string `mapstructure:"-"`
378+
CheckShadowing bool `mapstructure:"check-shadowing"`
378379
Settings map[string]map[string]interface{}
379380

380381
Enable []string
@@ -383,7 +384,7 @@ type GovetSettings struct {
383384
DisableAll bool `mapstructure:"disable-all"`
384385
}
385386

386-
func (cfg GovetSettings) Validate() error {
387+
func (cfg *GovetSettings) Validate() error {
387388
if cfg.EnableAll && cfg.DisableAll {
388389
return errors.New("enable-all and disable-all can't be combined")
389390
}

pkg/golinters/govet.go

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,46 @@ var (
119119
}
120120
)
121121

122+
func NewGovet(cfg *config.GovetSettings) *goanalysis.Linter {
123+
var settings map[string]map[string]interface{}
124+
if cfg != nil {
125+
settings = cfg.Settings
126+
}
127+
return goanalysis.NewLinter(
128+
"govet",
129+
"Vet examines Go source code and reports suspicious constructs, "+
130+
"such as Printf calls whose arguments do not align with the format string",
131+
analyzersFromConfig(cfg),
132+
settings,
133+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
134+
}
135+
136+
func analyzersFromConfig(cfg *config.GovetSettings) []*analysis.Analyzer {
137+
if cfg == nil {
138+
return defaultAnalyzers
139+
}
140+
141+
if cfg.CheckShadowing {
142+
// Keeping for backward compatibility.
143+
cfg.Enable = append(cfg.Enable, shadow.Analyzer.Name)
144+
}
145+
146+
var enabledAnalyzers []*analysis.Analyzer
147+
for _, a := range allAnalyzers {
148+
if isAnalyzerEnabled(a.Name, cfg, defaultAnalyzers) {
149+
enabledAnalyzers = append(enabledAnalyzers, a)
150+
}
151+
}
152+
153+
return enabledAnalyzers
154+
}
155+
122156
func isAnalyzerEnabled(name string, cfg *config.GovetSettings, defaultAnalyzers []*analysis.Analyzer) bool {
157+
if (name == nilness.Analyzer.Name || name == unusedwrite.Analyzer.Name) &&
158+
config.IsGreaterThanOrEqualGo118(cfg.Go) {
159+
return false
160+
}
161+
123162
if cfg.EnableAll {
124163
for _, n := range cfg.Disable {
125164
if n == name {
@@ -128,58 +167,28 @@ func isAnalyzerEnabled(name string, cfg *config.GovetSettings, defaultAnalyzers
128167
}
129168
return true
130169
}
170+
131171
// Raw for loops should be OK on small slice lengths.
132172
for _, n := range cfg.Enable {
133173
if n == name {
134174
return true
135175
}
136176
}
177+
137178
for _, n := range cfg.Disable {
138179
if n == name {
139180
return false
140181
}
141182
}
183+
142184
if cfg.DisableAll {
143185
return false
144186
}
187+
145188
for _, a := range defaultAnalyzers {
146189
if a.Name == name {
147190
return true
148191
}
149192
}
150193
return false
151194
}
152-
153-
func analyzersFromConfig(cfg *config.GovetSettings) []*analysis.Analyzer {
154-
if cfg == nil {
155-
return defaultAnalyzers
156-
}
157-
158-
if cfg.CheckShadowing {
159-
// Keeping for backward compatibility.
160-
cfg.Enable = append(cfg.Enable, shadow.Analyzer.Name)
161-
}
162-
163-
var enabledAnalyzers []*analysis.Analyzer
164-
for _, a := range allAnalyzers {
165-
if isAnalyzerEnabled(a.Name, cfg, defaultAnalyzers) {
166-
enabledAnalyzers = append(enabledAnalyzers, a)
167-
}
168-
}
169-
170-
return enabledAnalyzers
171-
}
172-
173-
func NewGovet(cfg *config.GovetSettings) *goanalysis.Linter {
174-
var settings map[string]map[string]interface{}
175-
if cfg != nil {
176-
settings = cfg.Settings
177-
}
178-
return goanalysis.NewLinter(
179-
"govet",
180-
"Vet examines Go source code and reports suspicious constructs, "+
181-
"such as Printf calls whose arguments do not align with the format string",
182-
analyzersFromConfig(cfg),
183-
settings,
184-
).WithLoadMode(goanalysis.LoadModeTypesInfo)
185-
}

pkg/lint/linter/config.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package linter
22

33
import (
4-
"strings"
5-
6-
hcversion "github.com/hashicorp/go-version"
74
"golang.org/x/tools/go/analysis"
85
"golang.org/x/tools/go/packages"
96

@@ -126,7 +123,7 @@ func (lc *Config) Name() string {
126123
}
127124

128125
func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
129-
if isGreaterThanOrEqualGo118(cfg) {
126+
if cfg != nil && config.IsGreaterThanOrEqualGo118(cfg.Run.Go) {
130127
lc.Linter = &Noop{
131128
name: lc.Linter.Name(),
132129
desc: lc.Linter.Desc(),
@@ -148,21 +145,3 @@ func NewConfig(linter Linter) *Config {
148145
}
149146
return lc.WithLoadFiles()
150147
}
151-
152-
func isGreaterThanOrEqualGo118(cfg *config.Config) bool {
153-
if cfg == nil {
154-
return false
155-
}
156-
157-
v1, err := hcversion.NewVersion(strings.TrimPrefix(cfg.Run.Go, "go"))
158-
if err != nil {
159-
return false
160-
}
161-
162-
limit, err := hcversion.NewVersion("1.18")
163-
if err != nil {
164-
return false
165-
}
166-
167-
return v1.GreaterThanOrEqual(limit)
168-
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
164164
unusedCfg = &m.cfg.LintersSettings.Unused
165165
varnamelenCfg = &m.cfg.LintersSettings.Varnamelen
166166
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
167+
168+
if govetCfg != nil {
169+
govetCfg.Go = m.cfg.Run.Go
170+
}
167171
}
168172

169173
const megacheckName = "megacheck"

0 commit comments

Comments
 (0)