Skip to content

Commit

Permalink
feat: disable copyloopvar and intrange on Go 1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 17, 2024
1 parent f4e33e0 commit 745a98a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
8 changes: 4 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ type Version struct {
Debug bool `mapstructure:"debug"`
}

func IsGreaterThanOrEqualGo122(v string) bool {
v1, err := hcversion.NewVersion(strings.TrimPrefix(v, "go"))
func IsGoGreaterThanOrEqual(current, limit string) bool {
v1, err := hcversion.NewVersion(strings.TrimPrefix(current, "go"))
if err != nil {
return false
}

limit, err := hcversion.NewVersion("1.22")
l, err := hcversion.NewVersion(limit)
if err != nil {
return false
}

return v1.GreaterThanOrEqual(limit)
return v1.GreaterThanOrEqual(l)
}

func DetectGoVersion() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/govet.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func analyzersFromConfig(settings *config.GovetSettings) []*analysis.Analyzer {

func isAnalyzerEnabled(name string, cfg *config.GovetSettings, defaultAnalyzers []*analysis.Analyzer) bool {
// TODO(ldez) remove loopclosure when go1.23
if name == loopclosure.Analyzer.Name && config.IsGreaterThanOrEqualGo122(cfg.Go) {
if name == loopclosure.Analyzer.Name && config.IsGoGreaterThanOrEqual(cfg.Go, "1.22") {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/paralleltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewParallelTest(settings *config.ParallelTestSettings) *goanalysis.Linter {
"ignoremissingsubtests": settings.IgnoreMissingSubtests,
}

if config.IsGreaterThanOrEqualGo122(settings.Go) {
if config.IsGoGreaterThanOrEqual(settings.Go, "1.22") {
d["ignoreloopVar"] = true
}

Expand Down
15 changes: 11 additions & 4 deletions pkg/lint/linter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ 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) {
func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) bool) *Config {
if cfg != nil && cond(cfg) {
lc.Linter = &Noop{
name: lc.Linter.Name(),
desc: lc.Linter.Desc(),
name: lc.Linter.Name(),
desc: lc.Linter.Desc(),
reason: "This linter is disabled because the Go version of your project is lower than Go 1.22.",
run: func(_ *analysis.Pass) (any, error) {
return nil, nil
},
Expand All @@ -150,6 +151,12 @@ func (lc *Config) WithNoopFallback(cfg *config.Config) *Config {
return lc
}

func IsGoLowerThan(limit string) func(cfg *config.Config) bool {
return func(cfg *config.Config) bool {
return cfg != nil && !config.IsGoGreaterThanOrEqual(cfg.Run.Go, limit)
}
}

func NewConfig(linter Linter) *Config {
lc := &Config{
Linter: linter,
Expand Down
12 changes: 7 additions & 5 deletions pkg/lint/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ type Linter interface {
}

type Noop struct {
name string
desc string
run func(pass *analysis.Pass) (any, error)
name string
desc string
reason string
run func(pass *analysis.Pass) (any, error)
}

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
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.IsGoLowerThan("1.22")),

linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
WithSince("v1.37.0").
Expand Down Expand Up @@ -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.IsGoLowerThan("1.22")),

linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
WithSince("v1.43.0").
Expand Down

0 comments on commit 745a98a

Please sign in to comment.