Skip to content

Commit 12ed5fa

Browse files
authored
staticcheck: configurable Go version. (#1946)
1 parent 54bfbb9 commit 12ed5fa

File tree

9 files changed

+96
-69
lines changed

9 files changed

+96
-69
lines changed

.golangci.example.yml

+16-7
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ linters-settings:
358358
per_char_threshold: "3.0"
359359
truncate: "32"
360360

361+
gosimple:
362+
# Select the Go version to target. The default is '1.13'.
363+
go: "1.15"
364+
361365
govet:
362366
# report about shadowed variables
363367
check-shadowing: true
@@ -488,6 +492,14 @@ linters-settings:
488492
- name: indent-error-flow
489493
severity: warning
490494

495+
staticcheck:
496+
# Select the Go version to target. The default is '1.13'.
497+
go: "1.15"
498+
499+
stylecheck:
500+
# Select the Go version to target. The default is '1.13'.
501+
go: "1.15"
502+
491503
tagliatelle:
492504
# check the struck tag name case
493505
case:
@@ -531,11 +543,8 @@ linters-settings:
531543
check-exported: false
532544

533545
unused:
534-
# treat code as a program (not a library) and report unused exported identifiers; default is false.
535-
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
536-
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
537-
# with golangci-lint call it on a directory with the changed file.
538-
check-exported: false
546+
# Select the Go version to target. The default is '1.13'.
547+
go: "1.15"
539548

540549
whitespace:
541550
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
@@ -556,8 +565,8 @@ linters-settings:
556565
force-short-decl-cuddling: false
557566
strict-append: true
558567

559-
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
560-
# for more info.
568+
# The custom section can be used to define linter plugins to be loaded at runtime.
569+
# See README doc for more info.
561570
custom:
562571
# Each custom linter should have a unique name.
563572
example:

pkg/config/linters_settings.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type LintersSettings struct {
104104
GoModDirectives GoModDirectivesSettings
105105
Gomodguard GoModGuardSettings
106106
Gosec GoSecSettings
107+
Gosimple StaticCheckSettings
107108
Govet GovetSettings
108109
Ifshort IfshortSettings
109110
ImportAs ImportAsSettings
@@ -119,12 +120,14 @@ type LintersSettings struct {
119120
Promlinter PromlinterSettings
120121
Revive ReviveSettings
121122
RowsErrCheck RowsErrCheckSettings
123+
Staticcheck StaticCheckSettings
122124
Structcheck StructCheckSettings
125+
Stylecheck StaticCheckSettings
123126
Tagliatelle TagliatelleSettings
124127
Testpackage TestpackageSettings
125128
Thelper ThelperSettings
126129
Unparam UnparamSettings
127-
Unused UnusedSettings
130+
Unused StaticCheckSettings
128131
Varcheck VarCheckSettings
129132
Whitespace WhitespaceSettings
130133
WSL WSLSettings
@@ -376,6 +379,10 @@ type RowsErrCheckSettings struct {
376379
Packages []string
377380
}
378381

382+
type StaticCheckSettings struct {
383+
GoVersion string `mapstructure:"go"`
384+
}
385+
379386
type StructCheckSettings struct {
380387
CheckExportedFields bool `mapstructure:"exported-fields"`
381388
}
@@ -414,10 +421,6 @@ type UnparamSettings struct {
414421
Algo string
415422
}
416423

417-
type UnusedSettings struct {
418-
CheckExported bool `mapstructure:"check-exported"`
419-
}
420-
421424
type VarCheckSettings struct {
422425
CheckExportedFields bool `mapstructure:"exported-fields"`
423426
}

pkg/golinters/gosimple.go

+3-3
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/megacheck.go

-30
This file was deleted.

pkg/golinters/staticcheck.go

+3-3
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

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package golinters
2+
3+
import (
4+
"golang.org/x/tools/go/analysis"
5+
6+
"github.com/golangci/golangci-lint/pkg/config"
7+
"github.com/golangci/golangci-lint/pkg/logutils"
8+
)
9+
10+
var debugf = logutils.Debug("megacheck")
11+
12+
func setupStaticCheckAnalyzers(m map[string]*analysis.Analyzer, settings *config.StaticCheckSettings) []*analysis.Analyzer {
13+
var ret []*analysis.Analyzer
14+
for _, v := range m {
15+
setAnalyzerGoVersion(v, settings)
16+
ret = append(ret, v)
17+
}
18+
return ret
19+
}
20+
21+
func setAnalyzerGoVersion(a *analysis.Analyzer, settings *config.StaticCheckSettings) {
22+
// TODO: uses "1.13" for backward compatibility, but in the future (v2) must be set by using build.Default.ReleaseTags like staticcheck.
23+
goVersion := "1.13"
24+
if settings != nil && settings.GoVersion != "" {
25+
goVersion = settings.GoVersion
26+
}
27+
28+
if v := a.Flags.Lookup("go"); v != nil {
29+
if err := v.Value.Set(goVersion); err != nil {
30+
debugf("Failed to set go version: %s", err)
31+
}
32+
}
33+
}

pkg/golinters/stylecheck.go

+3-3
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

+8-4
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

+22-14
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)