Skip to content

Commit

Permalink
feat(staticcheck): configurable Go version.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 29, 2021
1 parent 9a3a29a commit 1f0072c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 42 deletions.
25 changes: 20 additions & 5 deletions pkg/config/linters_settings.go
Expand Up @@ -75,6 +75,18 @@ var defaultLintersSettings = LintersSettings{
Forbidigo: ForbidigoSettings{
ExcludeGodocExamples: true,
},
Staticcheck: StaticCheckSettings{
GoVersion: "1.13",
},
Gosimple: StaticCheckSettings{
GoVersion: "1.13",
},
Stylecheck: StaticCheckSettings{
GoVersion: "1.13",
},
Unused: StaticCheckSettings{
GoVersion: "1.13",
},
}

type LintersSettings struct {
Expand Down Expand Up @@ -104,6 +116,7 @@ type LintersSettings struct {
GoModDirectives GoModDirectivesSettings
Gomodguard GoModGuardSettings
Gosec GoSecSettings
Gosimple StaticCheckSettings
Govet GovetSettings
Ifshort IfshortSettings
ImportAs ImportAsSettings
Expand All @@ -119,12 +132,14 @@ type LintersSettings struct {
Promlinter PromlinterSettings
Revive ReviveSettings
RowsErrCheck RowsErrCheckSettings
Staticcheck StaticCheckSettings
Structcheck StructCheckSettings
Stylecheck StaticCheckSettings
Tagliatelle TagliatelleSettings
Testpackage TestpackageSettings
Thelper ThelperSettings
Unparam UnparamSettings
Unused UnusedSettings
Unused StaticCheckSettings
Varcheck VarCheckSettings
Whitespace WhitespaceSettings
WSL WSLSettings
Expand Down Expand Up @@ -376,6 +391,10 @@ type RowsErrCheckSettings struct {
Packages []string
}

type StaticCheckSettings struct {
GoVersion string `mapstructure:"go"`
}

type StructCheckSettings struct {
CheckExportedFields bool `mapstructure:"exported-fields"`
}
Expand Down Expand Up @@ -414,10 +433,6 @@ type UnparamSettings struct {
Algo string
}

type UnusedSettings struct {
CheckExported bool `mapstructure:"check-exported"`
}

type VarCheckSettings struct {
CheckExportedFields bool `mapstructure:"exported-fields"`
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/golinters/gosimple.go
Expand Up @@ -3,12 +3,12 @@ package golinters
import (
"honnef.co/go/tools/simple"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewGosimple() *goanalysis.Linter {
analyzers := analyzersMapToSlice(simple.Analyzers)
setAnalyzersGoVersion(analyzers)
func NewGosimple(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := setupStaticCheckAnalyzers(simple.Analyzers, settings)

return goanalysis.NewLinter(
"gosimple",
Expand Down
6 changes: 3 additions & 3 deletions pkg/golinters/staticcheck.go
Expand Up @@ -3,12 +3,12 @@ package golinters
import (
"honnef.co/go/tools/staticcheck"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewStaticcheck() *goanalysis.Linter {
analyzers := analyzersMapToSlice(staticcheck.Analyzers)
setAnalyzersGoVersion(analyzers)
func NewStaticcheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := setupStaticCheckAnalyzers(staticcheck.Analyzers, settings)

return goanalysis.NewLinter(
"staticcheck",
Expand Down
22 changes: 12 additions & 10 deletions pkg/golinters/staticcheck_common.go
@@ -1,30 +1,32 @@
package golinters

import (
"fmt"

"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 analyzersMapToSlice(m map[string]*analysis.Analyzer) []*analysis.Analyzer {
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 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)
}
func setAnalyzerGoVersion(a *analysis.Analyzer, settings *config.StaticCheckSettings) {
goVersion := "1.13"
if settings != nil {
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)
}
}
}
6 changes: 3 additions & 3 deletions pkg/golinters/stylecheck.go
Expand Up @@ -3,12 +3,12 @@ package golinters
import (
"honnef.co/go/tools/stylecheck"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewStylecheck() *goanalysis.Linter {
analyzers := analyzersMapToSlice(stylecheck.Analyzers)
setAnalyzersGoVersion(analyzers)
func NewStylecheck(settings *config.StaticCheckSettings) *goanalysis.Linter {
analyzers := setupStaticCheckAnalyzers(stylecheck.Analyzers, settings)

return goanalysis.NewLinter(
"stylecheck",
Expand Down
12 changes: 8 additions & 4 deletions pkg/golinters/unused.go
Expand Up @@ -7,12 +7,17 @@ import (
"golang.org/x/tools/go/analysis"
"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/lint/linter"
"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"

var mu sync.Mutex
Expand Down Expand Up @@ -49,13 +54,12 @@ func NewUnused() *goanalysis.Linter {
},
}

analyzers := []*analysis.Analyzer{analyzer}
setAnalyzersGoVersion(analyzers)
setAnalyzerGoVersion(analyzer, settings)

lnt := goanalysis.NewLinter(
name,
"Checks Go code for unused constants, variables, functions and types",
analyzers,
[]*analysis.Analyzer{analyzer},
nil,
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
return resIssues
Expand Down
36 changes: 22 additions & 14 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -113,6 +113,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var goModDirectivesCfg *config.GoModDirectivesSettings
var tagliatelleCfg *config.TagliatelleSettings
var gosecCfg *config.GoSecSettings
var gosimpleCfg *config.StaticCheckSettings
var staticcheckCfg *config.StaticCheckSettings
var stylecheckCfg *config.StaticCheckSettings
var unusedCfg *config.StaticCheckSettings

if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
Expand All @@ -129,6 +133,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle
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"
Expand Down Expand Up @@ -166,28 +174,28 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs, linter.PresetSQL).
WithURL("https://github.com/jingyugao/rowserrcheck"),

linter.NewConfig(golinters.NewStaticcheck()).
linter.NewConfig(golinters.NewStaticcheck(staticcheckCfg)).
WithSince("v1.0.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs, linter.PresetMetaLinter).
WithAlternativeNames(megacheckName).
WithURL("https://staticcheck.io/"),
linter.NewConfig(golinters.NewUnused()).
linter.NewConfig(golinters.NewUnused(unusedCfg)).
WithSince("v1.20.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetUnused).
WithAlternativeNames(megacheckName).
ConsiderSlow().
WithChangeTypes().
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
linter.NewConfig(golinters.NewGosimple()).
linter.NewConfig(golinters.NewGosimple(gosimpleCfg)).
WithSince("v1.20.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
WithAlternativeNames(megacheckName).
WithURL("https://github.com/dominikh/go-tools/tree/master/simple"),

linter.NewConfig(golinters.NewStylecheck()).
linter.NewConfig(golinters.NewStylecheck(stylecheckCfg)).
WithSince("v1.20.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
Expand Down Expand Up @@ -499,16 +507,16 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
}

enabledByDefault := map[string]bool{
golinters.NewGovet(nil).Name(): true,
golinters.NewErrcheck().Name(): true,
golinters.NewStaticcheck().Name(): true,
golinters.NewUnused().Name(): true,
golinters.NewGosimple().Name(): true,
golinters.NewStructcheck().Name(): true,
golinters.NewVarcheck().Name(): true,
golinters.NewIneffassign().Name(): true,
golinters.NewDeadcode().Name(): true,
golinters.NewTypecheck().Name(): true,
golinters.NewGovet(nil).Name(): true,
golinters.NewErrcheck().Name(): true,
golinters.NewStaticcheck(staticcheckCfg).Name(): true,
golinters.NewUnused(unusedCfg).Name(): true,
golinters.NewGosimple(gosimpleCfg).Name(): true,
golinters.NewStructcheck().Name(): true,
golinters.NewVarcheck().Name(): true,
golinters.NewIneffassign().Name(): true,
golinters.NewDeadcode().Name(): true,
golinters.NewTypecheck().Name(): true,
}
return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
return enabledByDefault[lc.Name()]
Expand Down

0 comments on commit 1f0072c

Please sign in to comment.