staticcheck: propagate Go version (#4907)
This commit is contained in:
parent
ca0b09e5e3
commit
adbdfdb288
@ -9,6 +9,8 @@ import (
|
|||||||
"go/types"
|
"go/types"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
@ -150,12 +152,15 @@ func (lp *loadingPackage) loadFromSource(loadMode LoadMode) error {
|
|||||||
}
|
}
|
||||||
return imp.Types, nil
|
return imp.Types, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tc := &types.Config{
|
tc := &types.Config{
|
||||||
Importer: importerFunc(importer),
|
Importer: importerFunc(importer),
|
||||||
Error: func(err error) {
|
Error: func(err error) {
|
||||||
pkg.Errors = append(pkg.Errors, lp.convertError(err)...)
|
pkg.Errors = append(pkg.Errors, lp.convertError(err)...)
|
||||||
},
|
},
|
||||||
|
GoVersion: getGoVersion(),
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax)
|
_ = types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax)
|
||||||
// Don't handle error here: errors are adding by tc.Error function.
|
// 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())
|
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 {
|
func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
|
||||||
cfg := internal.StaticCheckConfig(settings)
|
cfg := internal.StaticCheckConfig(settings)
|
||||||
|
|
||||||
analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
|
analyzers := internal.SetupStaticCheckAnalyzers(simple.Analyzers, cfg.Checks)
|
||||||
|
|
||||||
return goanalysis.NewLinter(
|
return goanalysis.NewLinter(
|
||||||
"gosimple",
|
"gosimple",
|
||||||
|
@ -14,20 +14,7 @@ import (
|
|||||||
|
|
||||||
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)
|
var debugf = logutils.Debug(logutils.DebugKeyMegacheck)
|
||||||
|
|
||||||
func GetGoVersion(settings *config.StaticCheckSettings) string {
|
func SetupStaticCheckAnalyzers(src []*lint.Analyzer, checks []string) []*analysis.Analyzer {
|
||||||
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 {
|
|
||||||
var names []string
|
var names []string
|
||||||
for _, a := range src {
|
for _, a := range src {
|
||||||
names = append(names, a.Analyzer.Name)
|
names = append(names, a.Analyzer.Name)
|
||||||
@ -38,7 +25,6 @@ func SetupStaticCheckAnalyzers(src []*lint.Analyzer, goVersion string, checks []
|
|||||||
var ret []*analysis.Analyzer
|
var ret []*analysis.Analyzer
|
||||||
for _, a := range src {
|
for _, a := range src {
|
||||||
if filter[a.Analyzer.Name] {
|
if filter[a.Analyzer.Name] {
|
||||||
SetAnalyzerGoVersion(a.Analyzer, goVersion)
|
|
||||||
ret = append(ret, a.Analyzer)
|
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
|
module spancheck
|
||||||
|
|
||||||
go 1.20
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
go.opentelemetry.io/otel v1.21.0
|
go.opentelemetry.io/otel v1.21.0
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
|
func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
|
||||||
cfg := internal.StaticCheckConfig(settings)
|
cfg := internal.StaticCheckConfig(settings)
|
||||||
analyzers := internal.SetupStaticCheckAnalyzers(staticcheck.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
|
analyzers := internal.SetupStaticCheckAnalyzers(staticcheck.Analyzers, cfg.Checks)
|
||||||
|
|
||||||
return goanalysis.NewLinter(
|
return goanalysis.NewLinter(
|
||||||
"staticcheck",
|
"staticcheck",
|
||||||
|
@ -20,7 +20,7 @@ func New(settings *config.StaticCheckSettings) *goanalysis.Linter {
|
|||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, internal.GetGoVersion(settings), cfg.Checks)
|
analyzers := internal.SetupStaticCheckAnalyzers(stylecheck.Analyzers, cfg.Checks)
|
||||||
|
|
||||||
return goanalysis.NewLinter(
|
return goanalysis.NewLinter(
|
||||||
"stylecheck",
|
"stylecheck",
|
||||||
|
@ -12,14 +12,13 @@ import (
|
|||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/config"
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/goanalysis"
|
"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/lint/linter"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
const linterName = "unused"
|
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 mu sync.Mutex
|
||||||
var resIssues []goanalysis.Issue
|
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(
|
return goanalysis.NewLinter(
|
||||||
linterName,
|
linterName,
|
||||||
"Checks Go code for unused constants, variables, functions and types",
|
"Checks Go code for unused constants, variables, functions and types",
|
||||||
|
@ -774,7 +774,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
|
|||||||
WithLoadForGoAnalysis().
|
WithLoadForGoAnalysis().
|
||||||
WithURL("https://github.com/mvdan/unparam"),
|
WithURL("https://github.com/mvdan/unparam"),
|
||||||
|
|
||||||
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused, &cfg.LintersSettings.Staticcheck)).
|
linter.NewConfig(unused.New(&cfg.LintersSettings.Unused)).
|
||||||
WithEnabledByDefault().
|
WithEnabledByDefault().
|
||||||
WithSince("v1.20.0").
|
WithSince("v1.20.0").
|
||||||
WithLoadForGoAnalysis().
|
WithLoadForGoAnalysis().
|
||||||
|
Loading…
x
Reference in New Issue
Block a user