Skip to content

Commit 1f0072c

Browse files
committed
feat(staticcheck): configurable Go version.
1 parent 9a3a29a commit 1f0072c

File tree

7 files changed

+71
-42
lines changed

7 files changed

+71
-42
lines changed

pkg/config/linters_settings.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ var defaultLintersSettings = LintersSettings{
7575
Forbidigo: ForbidigoSettings{
7676
ExcludeGodocExamples: true,
7777
},
78+
Staticcheck: StaticCheckSettings{
79+
GoVersion: "1.13",
80+
},
81+
Gosimple: StaticCheckSettings{
82+
GoVersion: "1.13",
83+
},
84+
Stylecheck: StaticCheckSettings{
85+
GoVersion: "1.13",
86+
},
87+
Unused: StaticCheckSettings{
88+
GoVersion: "1.13",
89+
},
7890
}
7991

8092
type LintersSettings struct {
@@ -104,6 +116,7 @@ type LintersSettings struct {
104116
GoModDirectives GoModDirectivesSettings
105117
Gomodguard GoModGuardSettings
106118
Gosec GoSecSettings
119+
Gosimple StaticCheckSettings
107120
Govet GovetSettings
108121
Ifshort IfshortSettings
109122
ImportAs ImportAsSettings
@@ -119,12 +132,14 @@ type LintersSettings struct {
119132
Promlinter PromlinterSettings
120133
Revive ReviveSettings
121134
RowsErrCheck RowsErrCheckSettings
135+
Staticcheck StaticCheckSettings
122136
Structcheck StructCheckSettings
137+
Stylecheck StaticCheckSettings
123138
Tagliatelle TagliatelleSettings
124139
Testpackage TestpackageSettings
125140
Thelper ThelperSettings
126141
Unparam UnparamSettings
127-
Unused UnusedSettings
142+
Unused StaticCheckSettings
128143
Varcheck VarCheckSettings
129144
Whitespace WhitespaceSettings
130145
WSL WSLSettings
@@ -376,6 +391,10 @@ type RowsErrCheckSettings struct {
376391
Packages []string
377392
}
378393

394+
type StaticCheckSettings struct {
395+
GoVersion string `mapstructure:"go"`
396+
}
397+
379398
type StructCheckSettings struct {
380399
CheckExportedFields bool `mapstructure:"exported-fields"`
381400
}
@@ -414,10 +433,6 @@ type UnparamSettings struct {
414433
Algo string
415434
}
416435

417-
type UnusedSettings struct {
418-
CheckExported bool `mapstructure:"check-exported"`
419-
}
420-
421436
type VarCheckSettings struct {
422437
CheckExportedFields bool `mapstructure:"exported-fields"`
423438
}

pkg/golinters/gosimple.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package golinters
33
import (
44
"honnef.co/go/tools/simple"
55

6+
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
78
)
89

9-
func NewGosimple() *goanalysis.Linter {
10-
analyzers := analyzersMapToSlice(simple.Analyzers)
11-
setAnalyzersGoVersion(analyzers)
10+
func NewGosimple(settings *config.StaticCheckSettings) *goanalysis.Linter {
11+
analyzers := setupStaticCheckAnalyzers(simple.Analyzers, settings)
1212

1313
return goanalysis.NewLinter(
1414
"gosimple",

pkg/golinters/staticcheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package golinters
33
import (
44
"honnef.co/go/tools/staticcheck"
55

6+
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
78
)
89

9-
func NewStaticcheck() *goanalysis.Linter {
10-
analyzers := analyzersMapToSlice(staticcheck.Analyzers)
11-
setAnalyzersGoVersion(analyzers)
10+
func NewStaticcheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
11+
analyzers := setupStaticCheckAnalyzers(staticcheck.Analyzers, settings)
1212

1313
return goanalysis.NewLinter(
1414
"staticcheck",

pkg/golinters/staticcheck_common.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
package golinters
22

33
import (
4-
"fmt"
5-
64
"golang.org/x/tools/go/analysis"
75

6+
"github.com/golangci/golangci-lint/pkg/config"
87
"github.com/golangci/golangci-lint/pkg/logutils"
98
)
109

1110
var debugf = logutils.Debug("megacheck")
1211

13-
func analyzersMapToSlice(m map[string]*analysis.Analyzer) []*analysis.Analyzer {
12+
func setupStaticCheckAnalyzers(m map[string]*analysis.Analyzer, settings *config.StaticCheckSettings) []*analysis.Analyzer {
1413
var ret []*analysis.Analyzer
1514
for _, v := range m {
15+
setAnalyzerGoVersion(v, settings)
1616
ret = append(ret, v)
1717
}
1818
return ret
1919
}
2020

21-
func setAnalyzersGoVersion(analyzers []*analysis.Analyzer) {
22-
const goVersion = 13 // TODO
23-
for _, a := range analyzers {
24-
if v := a.Flags.Lookup("go"); v != nil {
25-
if err := v.Value.Set(fmt.Sprintf("1.%d", goVersion)); err != nil {
26-
debugf("Failed to set go version: %s", err)
27-
}
21+
func setAnalyzerGoVersion(a *analysis.Analyzer, settings *config.StaticCheckSettings) {
22+
goVersion := "1.13"
23+
if settings != nil {
24+
goVersion = settings.GoVersion
25+
}
26+
27+
if v := a.Flags.Lookup("go"); v != nil {
28+
if err := v.Value.Set(goVersion); err != nil {
29+
debugf("Failed to set go version: %s", err)
2830
}
2931
}
3032
}

pkg/golinters/stylecheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package golinters
33
import (
44
"honnef.co/go/tools/stylecheck"
55

6+
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
78
)
89

9-
func NewStylecheck() *goanalysis.Linter {
10-
analyzers := analyzersMapToSlice(stylecheck.Analyzers)
11-
setAnalyzersGoVersion(analyzers)
10+
func NewStylecheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
11+
analyzers := setupStaticCheckAnalyzers(stylecheck.Analyzers, settings)
1212

1313
return goanalysis.NewLinter(
1414
"stylecheck",

pkg/golinters/unused.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import (
77
"golang.org/x/tools/go/analysis"
88
"honnef.co/go/tools/unused"
99

10+
"github.com/golangci/golangci-lint/pkg/config"
1011
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1112
"github.com/golangci/golangci-lint/pkg/lint/linter"
1213
"github.com/golangci/golangci-lint/pkg/result"
1314
)
1415

15-
func NewUnused() *goanalysis.Linter {
16+
type UnusedSettings struct {
17+
GoVersion string
18+
}
19+
20+
func NewUnused(settings *config.StaticCheckSettings) *goanalysis.Linter {
1621
const name = "unused"
1722

1823
var mu sync.Mutex
@@ -49,13 +54,12 @@ func NewUnused() *goanalysis.Linter {
4954
},
5055
}
5156

52-
analyzers := []*analysis.Analyzer{analyzer}
53-
setAnalyzersGoVersion(analyzers)
57+
setAnalyzerGoVersion(analyzer, settings)
5458

5559
lnt := goanalysis.NewLinter(
5660
name,
5761
"Checks Go code for unused constants, variables, functions and types",
58-
analyzers,
62+
[]*analysis.Analyzer{analyzer},
5963
nil,
6064
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
6165
return resIssues

pkg/lint/lintersdb/manager.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
113113
var goModDirectivesCfg *config.GoModDirectivesSettings
114114
var tagliatelleCfg *config.TagliatelleSettings
115115
var gosecCfg *config.GoSecSettings
116+
var gosimpleCfg *config.StaticCheckSettings
117+
var staticcheckCfg *config.StaticCheckSettings
118+
var stylecheckCfg *config.StaticCheckSettings
119+
var unusedCfg *config.StaticCheckSettings
116120

117121
if m.cfg != nil {
118122
govetCfg = &m.cfg.LintersSettings.Govet
@@ -129,6 +133,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
129133
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
130134
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
131135
gosecCfg = &m.cfg.LintersSettings.Gosec
136+
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
137+
staticcheckCfg = &m.cfg.LintersSettings.Staticcheck
138+
stylecheckCfg = &m.cfg.LintersSettings.Stylecheck
139+
unusedCfg = &m.cfg.LintersSettings.Unused
132140
}
133141

134142
const megacheckName = "megacheck"
@@ -166,28 +174,28 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
166174
WithPresets(linter.PresetBugs, linter.PresetSQL).
167175
WithURL("https://github.com/jingyugao/rowserrcheck"),
168176

169-
linter.NewConfig(golinters.NewStaticcheck()).
177+
linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)).
170178
WithSince("v1.0.0").
171179
WithLoadForGoAnalysis().
172180
WithPresets(linter.PresetBugs, linter.PresetMetaLinter).
173181
WithAlternativeNames(megacheckName).
174182
WithURL("https://staticcheck.io/"),
175-
linter.NewConfig(golinters.NewUnused()).
183+
linter.NewConfig(golinters.NewUnused(unusedCfg)).
176184
WithSince("v1.20.0").
177185
WithLoadForGoAnalysis().
178186
WithPresets(linter.PresetUnused).
179187
WithAlternativeNames(megacheckName).
180188
ConsiderSlow().
181189
WithChangeTypes().
182190
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
183-
linter.NewConfig(golinters.NewGosimple()).
191+
linter.NewConfig(golinters.NewGosimple(gosimpleCfg)).
184192
WithSince("v1.20.0").
185193
WithLoadForGoAnalysis().
186194
WithPresets(linter.PresetStyle).
187195
WithAlternativeNames(megacheckName).
188196
WithURL("https://github.com/dominikh/go-tools/tree/master/simple"),
189197

190-
linter.NewConfig(golinters.NewStylecheck()).
198+
linter.NewConfig(golinters.NewStylecheck(stylecheckCfg)).
191199
WithSince("v1.20.0").
192200
WithLoadForGoAnalysis().
193201
WithPresets(linter.PresetStyle).
@@ -499,16 +507,16 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
499507
}
500508

501509
enabledByDefault := map[string]bool{
502-
golinters.NewGovet(nil).Name(): true,
503-
golinters.NewErrcheck().Name(): true,
504-
golinters.NewStaticcheck().Name(): true,
505-
golinters.NewUnused().Name(): true,
506-
golinters.NewGosimple().Name(): true,
507-
golinters.NewStructcheck().Name(): true,
508-
golinters.NewVarcheck().Name(): true,
509-
golinters.NewIneffassign().Name(): true,
510-
golinters.NewDeadcode().Name(): true,
511-
golinters.NewTypecheck().Name(): true,
510+
golinters.NewGovet(nil).Name(): true,
511+
golinters.NewErrcheck().Name(): true,
512+
golinters.NewStaticcheck(staticcheckCfg).Name(): true,
513+
golinters.NewUnused(unusedCfg).Name(): true,
514+
golinters.NewGosimple(gosimpleCfg).Name(): true,
515+
golinters.NewStructcheck().Name(): true,
516+
golinters.NewVarcheck().Name(): true,
517+
golinters.NewIneffassign().Name(): true,
518+
golinters.NewDeadcode().Name(): true,
519+
golinters.NewTypecheck().Name(): true,
512520
}
513521
return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
514522
return enabledByDefault[lc.Name()]

0 commit comments

Comments
 (0)