staticcheck: propagate Go version (#4907)
This commit is contained in:
parent
ca0b09e5e3
commit
adbdfdb288
@ -9,6 +9,8 @@ import (
|
||||
"go/types"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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]
|
||||
}
|
||||
|
@ -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",
|
||||
|
@ -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)
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
2
pkg/golinters/spancheck/testdata/go.mod
vendored
2
pkg/golinters/spancheck/testdata/go.mod
vendored
@ -1,6 +1,6 @@
|
||||
module spancheck
|
||||
|
||||
go 1.20
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
go.opentelemetry.io/otel v1.21.0
|
||||
|
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
||||
@ -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",
|
||||
|
@ -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().
|
||||
|
Loading…
x
Reference in New Issue
Block a user