Skip to content

Commit adbdfdb

Browse files
authored
staticcheck: propagate Go version (#4907)
1 parent ca0b09e commit adbdfdb

File tree

8 files changed

+26
-24
lines changed

8 files changed

+26
-24
lines changed

pkg/goanalysis/runner_loadingpackage.go

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"go/types"
1010
"os"
1111
"reflect"
12+
"runtime"
13+
"strings"
1214
"sync"
1315
"sync/atomic"
1416

@@ -150,12 +152,15 @@ func (lp *loadingPackage) loadFromSource(loadMode LoadMode) error {
150152
}
151153
return imp.Types, nil
152154
}
155+
153156
tc := &types.Config{
154157
Importer: importerFunc(importer),
155158
Error: func(err error) {
156159
pkg.Errors = append(pkg.Errors, lp.convertError(err)...)
157160
},
161+
GoVersion: getGoVersion(),
158162
}
163+
159164
_ = types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax)
160165
// Don't handle error here: errors are adding by tc.Error function.
161166

@@ -497,3 +502,17 @@ func sizeOfReflectValueTreeBytes(rv reflect.Value, visitedPtrs map[uintptr]struc
497502
panic("unknown rv of type " + rv.String())
498503
}
499504
}
505+
506+
// TODO(ldez) temporary workaround
507+
func getGoVersion() string {
508+
goVersion := runtime.Version()
509+
510+
parts := strings.Fields(goVersion)
511+
512+
if len(parts) == 0 {
513+
return goVersion
514+
}
515+
516+
// When using GOEXPERIMENT, the version returned might look something like "go1.23.0 X:boringcrypto".
517+
return parts[0]
518+
}

pkg/golinters/gosimple/gosimple.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
1212
cfg := internal.StaticCheckConfig(settings)
1313

14-
analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
14+
analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, cfg.Checks)
1515

1616
return goanalysis.NewLinter(
1717
"gosimple",

pkg/golinters/internal/staticcheck_common.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,7 @@ import (
1414

1515
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)
1616

17-
func GetGoVersion(settings *config.StaticCheckSettings) string {
18-
var goVersion string
19-
if settings != nil {
20-
goVersion = settings.GoVersion
21-
}
22-
23-
if goVersion != "" {
24-
return goVersion
25-
}
26-
27-
return "1.17"
28-
}
29-
30-
func SetupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []string) []*analysis.Analyzer {
17+
func SetupStaticCheckAnalyzers(src []*lint.Analyzer, checks []string) []*analysis.Analyzer {
3118
var names []string
3219
for _, a := range src {
3320
names = append(names, a.Analyzer.Name)
@@ -38,7 +25,6 @@ func SetupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []
3825
var ret []*analysis.Analyzer
3926
for _, a := range src {
4027
if filter[a.Analyzer.Name] {
41-
SetAnalyzerGoVersion(a.Analyzer, goVersion)
4228
ret = append(ret, a.Analyzer)
4329
}
4430
}

pkg/golinters/spancheck/testdata/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module spancheck
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
go.opentelemetry.io/otel v1.21.0

pkg/golinters/staticcheck/staticcheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

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

1515
return goanalysis.NewLinter(
1616
"staticcheck",

pkg/golinters/stylecheck/stylecheck.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
2020
return cfg, nil
2121
}
2222

23-
analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
23+
analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, cfg.Checks)
2424

2525
return goanalysis.NewLinter(
2626
"stylecheck",

pkg/golinters/unused/unused.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import (
1212

1313
"github.com/golangci/golangci-lint/pkg/config"
1414
"github.com/golangci/golangci-lint/pkg/goanalysis"
15-
"github.com/golangci/golangci-lint/pkg/golinters/internal"
1615
"github.com/golangci/golangci-lint/pkg/lint/linter"
1716
"github.com/golangci/golangci-lint/pkg/result"
1817
)
1918

2019
const linterName = "unused"
2120

22-
func New(settings *config.UnusedSettings, scSettings *config.StaticCheckSettings) *goanalysis.Linter {
21+
func New(settings *config.UnusedSettings) *goanalysis.Linter {
2322
var mu sync.Mutex
2423
var resIssues []goanalysis.Issue
2524

@@ -41,8 +40,6 @@ func New(settings *config.UnusedSettings, scSettings *config.StaticCheckSettings
4140
},
4241
}
4342

44-
internal.SetAnalyzerGoVersion(analyzer, internal.GetGoVersion(scSettings))
45-
4643
return goanalysis.NewLinter(
4744
linterName,
4845
"Checks Go code for unused constants, variables, functions and types",

pkg/lint/lintersdb/builder_linter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
774774
WithLoadForGoAnalysis().
775775
WithURL("https://github.com/mvdan/unparam"),
776776

777-
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused, &cfg.LintersSettings.Staticcheck)).
777+
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused)).
778778
WithEnabledByDefault().
779779
WithSince("v1.20.0").
780780
WithLoadForGoAnalysis().

0 commit comments

Comments
 (0)