go1.18 support (#2438)

This commit is contained in:
Ludovic Fernandez 2022-03-18 14:58:24 +01:00 committed by GitHub
commit 0c0804c6a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 197 additions and 118 deletions

View file

@ -1,7 +1,13 @@
package linter
import (
"strings"
hcversion "github.com/hashicorp/go-version"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/pkg/config"
)
const (
@ -119,9 +125,41 @@ func (lc *Config) Name() string {
return lc.Linter.Name()
}
func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
if isGreaterThanOrEqualGo118(cfg) {
lc.Linter = &Noop{
name: lc.Linter.Name(),
desc: lc.Linter.Desc(),
run: func(pass *analysis.Pass) (interface{}, error) {
return nil, nil
},
}
}
return lc
}
func NewConfig(linter Linter) *Config {
lc := &Config{
Linter: linter,
}
return lc.WithLoadFiles()
}
func isGreaterThanOrEqualGo118(cfg *config.Config) bool {
if cfg == nil {
return false
}
v1, err := hcversion.NewVersion(strings.TrimPrefix(cfg.Run.Go, "go"))
if err != nil {
return false
}
limit, err := hcversion.NewVersion("1.18")
if err != nil {
return false
}
return v1.GreaterThanOrEqual(limit)
}

View file

@ -3,6 +3,8 @@ package linter
import (
"context"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -11,3 +13,24 @@ type Linter interface {
Name() string
Desc() string
}
type Noop struct {
name string
desc string
run func(pass *analysis.Pass) (interface{}, error)
}
func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) {
lintCtx.Log.Warnf("%s is disabled because of go1.18."+
" If you are not using go1.18, you can set `go: go1.17` in the `run` section."+
" You can track the evolution of the go1.18 support by following the https://github.com/golangci/golangci-lint/issues/2649.", n.name)
return nil, nil
}
func (n Noop) Name() string {
return n.name
}
func (n Noop) Desc() string {
return n.desc
}