staticcheck: configurable Go version. (#1946)

This commit is contained in:
Ludovic Fernandez 2021-04-30 22:28:50 +02:00 committed by GitHub
parent 54bfbb9a52
commit 12ed5facc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 96 additions and 69 deletions

View File

@ -358,6 +358,10 @@ linters-settings:
per_char_threshold: "3.0" per_char_threshold: "3.0"
truncate: "32" truncate: "32"
gosimple:
# Select the Go version to target. The default is '1.13'.
go: "1.15"
govet: govet:
# report about shadowed variables # report about shadowed variables
check-shadowing: true check-shadowing: true
@ -488,6 +492,14 @@ linters-settings:
- name: indent-error-flow - name: indent-error-flow
severity: warning severity: warning
staticcheck:
# Select the Go version to target. The default is '1.13'.
go: "1.15"
stylecheck:
# Select the Go version to target. The default is '1.13'.
go: "1.15"
tagliatelle: tagliatelle:
# check the struck tag name case # check the struck tag name case
case: case:
@ -531,11 +543,8 @@ linters-settings:
check-exported: false check-exported: false
unused: unused:
# treat code as a program (not a library) and report unused exported identifiers; default is false. # Select the Go version to target. The default is '1.13'.
# XXX: if you enable this setting, unused will report a lot of false-positives in text editors: go: "1.15"
# if it's called for subdir of a project it can't find funcs usages. All text editor integrations
# with golangci-lint call it on a directory with the changed file.
check-exported: false
whitespace: whitespace:
multi-if: false # Enforces newlines (or comments) after every multi-line if statement multi-if: false # Enforces newlines (or comments) after every multi-line if statement
@ -556,8 +565,8 @@ linters-settings:
force-short-decl-cuddling: false force-short-decl-cuddling: false
strict-append: true strict-append: true
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc # The custom section can be used to define linter plugins to be loaded at runtime.
# for more info. # See README doc for more info.
custom: custom:
# Each custom linter should have a unique name. # Each custom linter should have a unique name.
example: example:

View File

@ -104,6 +104,7 @@ type LintersSettings struct {
GoModDirectives GoModDirectivesSettings GoModDirectives GoModDirectivesSettings
Gomodguard GoModGuardSettings Gomodguard GoModGuardSettings
Gosec GoSecSettings Gosec GoSecSettings
Gosimple StaticCheckSettings
Govet GovetSettings Govet GovetSettings
Ifshort IfshortSettings Ifshort IfshortSettings
ImportAs ImportAsSettings ImportAs ImportAsSettings
@ -119,12 +120,14 @@ type LintersSettings struct {
Promlinter PromlinterSettings Promlinter PromlinterSettings
Revive ReviveSettings Revive ReviveSettings
RowsErrCheck RowsErrCheckSettings RowsErrCheck RowsErrCheckSettings
Staticcheck StaticCheckSettings
Structcheck StructCheckSettings Structcheck StructCheckSettings
Stylecheck StaticCheckSettings
Tagliatelle TagliatelleSettings Tagliatelle TagliatelleSettings
Testpackage TestpackageSettings Testpackage TestpackageSettings
Thelper ThelperSettings Thelper ThelperSettings
Unparam UnparamSettings Unparam UnparamSettings
Unused UnusedSettings Unused StaticCheckSettings
Varcheck VarCheckSettings Varcheck VarCheckSettings
Whitespace WhitespaceSettings Whitespace WhitespaceSettings
WSL WSLSettings WSL WSLSettings
@ -376,6 +379,10 @@ type RowsErrCheckSettings struct {
Packages []string Packages []string
} }
type StaticCheckSettings struct {
GoVersion string `mapstructure:"go"`
}
type StructCheckSettings struct { type StructCheckSettings struct {
CheckExportedFields bool `mapstructure:"exported-fields"` CheckExportedFields bool `mapstructure:"exported-fields"`
} }
@ -414,10 +421,6 @@ type UnparamSettings struct {
Algo string Algo string
} }
type UnusedSettings struct {
CheckExported bool `mapstructure:"check-exported"`
}
type VarCheckSettings struct { type VarCheckSettings struct {
CheckExportedFields bool `mapstructure:"exported-fields"` CheckExportedFields bool `mapstructure:"exported-fields"`
} }

View File

@ -3,12 +3,12 @@ package golinters
import ( import (
"honnef.co/go/tools/simple" "honnef.co/go/tools/simple"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
) )
func NewGosimple() *goanalysis.Linter { func NewGosimple(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := analyzersMapToSlice(simple.Analyzers) analyzers := setupStaticCheckAnalyzers(simple.Analyzers, settings)
setAnalyzersGoVersion(analyzers)
return goanalysis.NewLinter( return goanalysis.NewLinter(
"gosimple", "gosimple",

View File

@ -1,30 +0,0 @@
package golinters
import (
"fmt"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/logutils"
)
var debugf = logutils.Debug("megacheck")
func analyzersMapToSlice(m map[string]*analysis.Analyzer) []*analysis.Analyzer {
var ret []*analysis.Analyzer
for _, v := range m {
ret = append(ret, v)
}
return ret
}
func setAnalyzersGoVersion(analyzers []*analysis.Analyzer) {
const goVersion = 13 // TODO
for _, a := range analyzers {
if v := a.Flags.Lookup("go"); v != nil {
if err := v.Value.Set(fmt.Sprintf("1.%d", goVersion)); err != nil {
debugf("Failed to set go version: %s", err)
}
}
}
}

View File

@ -3,12 +3,12 @@ package golinters
import ( import (
"honnef.co/go/tools/staticcheck" "honnef.co/go/tools/staticcheck"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
) )
func NewStaticcheck() *goanalysis.Linter { func NewStaticcheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := analyzersMapToSlice(staticcheck.Analyzers) analyzers := setupStaticCheckAnalyzers(staticcheck.Analyzers, settings)
setAnalyzersGoVersion(analyzers)
return goanalysis.NewLinter( return goanalysis.NewLinter(
"staticcheck", "staticcheck",

View File

@ -0,0 +1,33 @@
package golinters
import (
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
)
var debugf = logutils.Debug("megacheck")
func setupStaticCheckAnalyzers(m map[string]*analysis.Analyzer, settings *config.StaticCheckSettings) []*analysis.Analyzer {
var ret []*analysis.Analyzer
for _, v := range m {
setAnalyzerGoVersion(v, settings)
ret = append(ret, v)
}
return ret
}
func setAnalyzerGoVersion(a *analysis.Analyzer, settings *config.StaticCheckSettings) {
// TODO: uses "1.13" for backward compatibility, but in the future (v2) must be set by using build.Default.ReleaseTags like staticcheck.
goVersion := "1.13"
if settings != nil && settings.GoVersion != "" {
goVersion = settings.GoVersion
}
if v := a.Flags.Lookup("go"); v != nil {
if err := v.Value.Set(goVersion); err != nil {
debugf("Failed to set go version: %s", err)
}
}
}

View File

@ -3,12 +3,12 @@ package golinters
import ( import (
"honnef.co/go/tools/stylecheck" "honnef.co/go/tools/stylecheck"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
) )
func NewStylecheck() *goanalysis.Linter { func NewStylecheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := analyzersMapToSlice(stylecheck.Analyzers) analyzers := setupStaticCheckAnalyzers(stylecheck.Analyzers, settings)
setAnalyzersGoVersion(analyzers)
return goanalysis.NewLinter( return goanalysis.NewLinter(
"stylecheck", "stylecheck",

View File

@ -7,12 +7,17 @@ import (
"golang.org/x/tools/go/analysis" "golang.org/x/tools/go/analysis"
"honnef.co/go/tools/unused" "honnef.co/go/tools/unused"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result" "github.com/golangci/golangci-lint/pkg/result"
) )
func NewUnused() *goanalysis.Linter { type UnusedSettings struct {
GoVersion string
}
func NewUnused(settings *config.StaticCheckSettings) *goanalysis.Linter {
const name = "unused" const name = "unused"
var mu sync.Mutex var mu sync.Mutex
@ -49,13 +54,12 @@ func NewUnused() *goanalysis.Linter {
}, },
} }
analyzers := []*analysis.Analyzer{analyzer} setAnalyzerGoVersion(analyzer, settings)
setAnalyzersGoVersion(analyzers)
lnt := goanalysis.NewLinter( lnt := goanalysis.NewLinter(
name, name,
"Checks Go code for unused constants, variables, functions and types", "Checks Go code for unused constants, variables, functions and types",
analyzers, []*analysis.Analyzer{analyzer},
nil, nil,
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue { ).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
return resIssues return resIssues

View File

@ -113,6 +113,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var goModDirectivesCfg *config.GoModDirectivesSettings var goModDirectivesCfg *config.GoModDirectivesSettings
var tagliatelleCfg *config.TagliatelleSettings var tagliatelleCfg *config.TagliatelleSettings
var gosecCfg *config.GoSecSettings var gosecCfg *config.GoSecSettings
var gosimpleCfg *config.StaticCheckSettings
var staticcheckCfg *config.StaticCheckSettings
var stylecheckCfg *config.StaticCheckSettings
var unusedCfg *config.StaticCheckSettings
if m.cfg != nil { if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet govetCfg = &m.cfg.LintersSettings.Govet
@ -129,6 +133,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
gosecCfg = &m.cfg.LintersSettings.Gosec gosecCfg = &m.cfg.LintersSettings.Gosec
gosimpleCfg = &m.cfg.LintersSettings.Gosimple
staticcheckCfg = &m.cfg.LintersSettings.Staticcheck
stylecheckCfg = &m.cfg.LintersSettings.Stylecheck
unusedCfg = &m.cfg.LintersSettings.Unused
} }
const megacheckName = "megacheck" const megacheckName = "megacheck"
@ -166,13 +174,13 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs, linter.PresetSQL). WithPresets(linter.PresetBugs, linter.PresetSQL).
WithURL("https://github.com/jingyugao/rowserrcheck"), WithURL("https://github.com/jingyugao/rowserrcheck"),
linter.NewConfig(golinters.NewStaticcheck()). linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)).
WithSince("v1.0.0"). WithSince("v1.0.0").
WithLoadForGoAnalysis(). WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs, linter.PresetMetaLinter). WithPresets(linter.PresetBugs, linter.PresetMetaLinter).
WithAlternativeNames(megacheckName). WithAlternativeNames(megacheckName).
WithURL("https://staticcheck.io/"), WithURL("https://staticcheck.io/"),
linter.NewConfig(golinters.NewUnused()). linter.NewConfig(golinters.NewUnused(unusedCfg)).
WithSince("v1.20.0"). WithSince("v1.20.0").
WithLoadForGoAnalysis(). WithLoadForGoAnalysis().
WithPresets(linter.PresetUnused). WithPresets(linter.PresetUnused).
@ -180,14 +188,14 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
ConsiderSlow(). ConsiderSlow().
WithChangeTypes(). WithChangeTypes().
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"), WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
linter.NewConfig(golinters.NewGosimple()). linter.NewConfig(golinters.NewGosimple(gosimpleCfg)).
WithSince("v1.20.0"). WithSince("v1.20.0").
WithLoadForGoAnalysis(). WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle). WithPresets(linter.PresetStyle).
WithAlternativeNames(megacheckName). WithAlternativeNames(megacheckName).
WithURL("https://github.com/dominikh/go-tools/tree/master/simple"), WithURL("https://github.com/dominikh/go-tools/tree/master/simple"),
linter.NewConfig(golinters.NewStylecheck()). linter.NewConfig(golinters.NewStylecheck(stylecheckCfg)).
WithSince("v1.20.0"). WithSince("v1.20.0").
WithLoadForGoAnalysis(). WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle). WithPresets(linter.PresetStyle).
@ -501,9 +509,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
enabledByDefault := map[string]bool{ enabledByDefault := map[string]bool{
golinters.NewGovet(nil).Name(): true, golinters.NewGovet(nil).Name(): true,
golinters.NewErrcheck().Name(): true, golinters.NewErrcheck().Name(): true,
golinters.NewStaticcheck().Name(): true, golinters.NewStaticcheck(staticcheckCfg).Name(): true,
golinters.NewUnused().Name(): true, golinters.NewUnused(unusedCfg).Name(): true,
golinters.NewGosimple().Name(): true, golinters.NewGosimple(gosimpleCfg).Name(): true,
golinters.NewStructcheck().Name(): true, golinters.NewStructcheck().Name(): true,
golinters.NewVarcheck().Name(): true, golinters.NewVarcheck().Name(): true,
golinters.NewIneffassign().Name(): true, golinters.NewIneffassign().Name(): true,