feat: disable copyloopvar and intrange on Go < 1.22 ()

This commit is contained in:
Ludovic Fernandez 2024-02-19 14:58:58 +01:00 committed by GitHub
parent c65868c105
commit 64492b5e59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 148 additions and 32 deletions
pkg/lint

View file

@ -1,7 +1,8 @@
package linter
import (
"golang.org/x/tools/go/analysis"
"fmt"
"golang.org/x/tools/go/packages"
"github.com/golangci/golangci-lint/pkg/config"
@ -133,23 +134,27 @@ func (lc *Config) Name() string {
return lc.Linter.Name()
}
func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
if cfg != nil && config.IsGreaterThanOrEqualGo122(cfg.Run.Go) {
lc.Linter = &Noop{
name: lc.Linter.Name(),
desc: lc.Linter.Desc(),
run: func(_ *analysis.Pass) (any, error) {
return nil, nil
},
}
func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) error) *Config {
if err := cond(cfg); err != nil {
lc.Linter = NewNoop(lc.Linter, err.Error())
lc.LoadMode = 0
return lc.WithLoadFiles()
}
return lc
}
func IsGoLowerThanGo122() func(cfg *config.Config) error {
return func(cfg *config.Config) error {
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, "1.22") {
return nil
}
return fmt.Errorf("this linter is disabled because the Go version (%s) of your project is lower than Go 1.22", cfg.Run.Go)
}
}
func NewConfig(linter Linter) *Config {
lc := &Config{
Linter: linter,

View file

@ -3,8 +3,6 @@ package linter
import (
"context"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -15,14 +13,23 @@ type Linter interface {
}
type Noop struct {
name string
desc string
run func(pass *analysis.Pass) (any, error)
name string
desc string
reason string
}
func NewNoop(l Linter, reason string) Noop {
return Noop{
name: l.Name(),
desc: l.Desc(),
reason: reason,
}
}
func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) {
lintCtx.Log.Warnf("%s is disabled because of generics."+
" You can track the evolution of the generics support by following the https://github.com/golangci/golangci-lint/issues/2649.", n.name)
if n.reason != "" {
lintCtx.Log.Warnf("%s: %s", n.name, n.reason)
}
return nil, nil
}

View file

@ -303,7 +303,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewCopyLoopVar()).
WithSince("v1.57.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/karamaru-alpha/copyloopvar"),
WithURL("https://github.com/karamaru-alpha/copyloopvar").
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
WithSince("v1.37.0").
@ -617,7 +618,8 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewIntrange()).
WithSince("v1.57.0").
WithURL("https://github.com/ckaznocha/intrange"),
WithURL("https://github.com/ckaznocha/intrange").
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),
linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
WithSince("v1.43.0").