Skip to content

Commit

Permalink
staticcheck: propagate Go version (#4907)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Aug 14, 2024
1 parent ca0b09e commit adbdfdb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
19 changes: 19 additions & 0 deletions pkg/goanalysis/runner_loadingpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"go/types"
"os"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -150,12 +152,15 @@ func (lp *loadingPackage) loadFromSource(loadMode LoadMode) error {
}
return imp.Types, nil
}

tc := &types.Config{
Importer: importerFunc(importer),
Error: func(err error) {
pkg.Errors = append(pkg.Errors, lp.convertError(err)...)
},
GoVersion: getGoVersion(),
}

_ = types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax)
// Don't handle error here: errors are adding by tc.Error function.

Expand Down Expand Up @@ -497,3 +502,17 @@ func sizeOfReflectValueTreeBytes(rv reflect.Value, visitedPtrs map[uintptr]struc
panic("unknown rv of type " + rv.String())
}
}

// TODO(ldez) temporary workaround
func getGoVersion() string {
goVersion := runtime.Version()

parts := strings.Fields(goVersion)

if len(parts) == 0 {
return goVersion
}

// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
return parts[0]
}
2 changes: 1 addition & 1 deletion pkg/golinters/gosimple/gosimple.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
cfg := internal.StaticCheckConfig(settings)

analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, cfg.Checks)

return goanalysis.NewLinter(
"gosimple",
Expand Down
16 changes: 1 addition & 15 deletions pkg/golinters/internal/staticcheck_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ import (

var debugf = logutils.Debug(logutils.DebugKeyMegacheck)

func GetGoVersion(settings *config.StaticCheckSettings) string {
var goVersion string
if settings != nil {
goVersion = settings.GoVersion
}

if goVersion != "" {
return goVersion
}

return "1.17"
}

func SetupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []string) []*analysis.Analyzer {
func SetupStaticCheckAnalyzers(src []*lint.Analyzer, checks []string) []*analysis.Analyzer {
var names []string
for _, a := range src {
names = append(names, a.Analyzer.Name)
Expand All @@ -38,7 +25,6 @@ func SetupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []
var ret []*analysis.Analyzer
for _, a := range src {
if filter[a.Analyzer.Name] {
SetAnalyzerGoVersion(a.Analyzer, goVersion)
ret = append(ret, a.Analyzer)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/spancheck/testdata/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module spancheck

go 1.20
go 1.21

require (
go.opentelemetry.io/otel v1.21.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/staticcheck/staticcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
cfg := internal.StaticCheckConfig(settings)
analyzers := internal.SetupStaticCheckAnalyzers(staticcheck.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
analyzers := internal.SetupStaticCheckAnalyzers(staticcheck.Analyzers, cfg.Checks)

return goanalysis.NewLinter(
"staticcheck",
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/stylecheck/stylecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
return cfg, nil
}

analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, cfg.Checks)

return goanalysis.NewLinter(
"stylecheck",
Expand Down
5 changes: 1 addition & 4 deletions pkg/golinters/unused/unused.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (

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

const linterName = "unused"

func New(settings *config.UnusedSettings, scSettings *config.StaticCheckSettings) *goanalysis.Linter {
func New(settings *config.UnusedSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue

Expand All @@ -41,8 +40,6 @@ func New(settings *config.UnusedSettings, scSettings *config.StaticCheckSettings
},
}

internal.SetAnalyzerGoVersion(analyzer, internal.GetGoVersion(scSettings))

return goanalysis.NewLinter(
linterName,
"Checks Go code for unused constants, variables, functions and types",
Expand Down
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/mvdan/unparam"),

linter.NewConfig(unused.New(&cfg.LintersSettings.Unused, &cfg.LintersSettings.Staticcheck)).
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused)).
WithEnabledByDefault().
WithSince("v1.20.0").
WithLoadForGoAnalysis().
Expand Down

0 comments on commit adbdfdb

Please # to comment.