Skip to content

Commit 3e50805

Browse files
committed
feat: rewrite fallback condition
1 parent 94c8a50 commit 3e50805

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

pkg/lint/linter/config.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package linter
22

33
import (
4-
"golang.org/x/tools/go/analysis"
4+
"errors"
5+
56
"golang.org/x/tools/go/packages"
67

78
"github.com/golangci/golangci-lint/pkg/config"
@@ -133,27 +134,24 @@ func (lc *Config) Name() string {
133134
return lc.Linter.Name()
134135
}
135136

136-
func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) bool) *Config {
137-
if cfg != nil && cond(cfg) {
138-
lc.Linter = &Noop{
139-
name: lc.Linter.Name(),
140-
desc: lc.Linter.Desc(),
141-
reason: "This linter is disabled because the Go version of your project is lower than Go 1.22.",
142-
run: func(_ *analysis.Pass) (any, error) {
143-
return nil, nil
144-
},
145-
}
146-
137+
func (lc *Config) WithNoopFallback(cfg *config.Config, cond func(cfg *config.Config) error) *Config {
138+
if err := cond(cfg); err != nil {
139+
lc.Linter = NewNoop(lc.Linter, err.Error())
147140
lc.LoadMode = 0
141+
148142
return lc.WithLoadFiles()
149143
}
150144

151145
return lc
152146
}
153147

154-
func IsGoLowerThan(limit string) func(cfg *config.Config) bool {
155-
return func(cfg *config.Config) bool {
156-
return cfg != nil && !config.IsGoGreaterThanOrEqual(cfg.Run.Go, limit)
148+
func IsGoLowerThanGo122() func(cfg *config.Config) error {
149+
return func(cfg *config.Config) error {
150+
if cfg == nil || config.IsGoGreaterThanOrEqual(cfg.Run.Go, "1.22") {
151+
return nil
152+
}
153+
154+
return errors.New("this linter is disabled because the Go version of your project is lower than Go 1.22")
157155
}
158156
}
159157

pkg/lint/linter/linter.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package linter
33
import (
44
"context"
55

6-
"golang.org/x/tools/go/analysis"
7-
86
"github.com/golangci/golangci-lint/pkg/result"
97
)
108

@@ -18,7 +16,14 @@ type Noop struct {
1816
name string
1917
desc string
2018
reason string
21-
run func(pass *analysis.Pass) (any, error)
19+
}
20+
21+
func NewNoop(l Linter, reason string) *Noop {
22+
return &Noop{
23+
name: l.Name(),
24+
desc: l.Desc(),
25+
reason: reason,
26+
}
2227
}
2328

2429
func (n Noop) Run(_ context.Context, lintCtx *Context) ([]result.Issue, error) {

pkg/lint/lintersdb/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
304304
WithSince("v1.57.0").
305305
WithPresets(linter.PresetStyle).
306306
WithURL("https://github.com/karamaru-alpha/copyloopvar").
307-
WithNoopFallback(m.cfg, linter.IsGoLowerThan("1.22")),
307+
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),
308308

309309
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
310310
WithSince("v1.37.0").
@@ -619,7 +619,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
619619
linter.NewConfig(golinters.NewIntrange()).
620620
WithSince("v1.57.0").
621621
WithURL("https://github.com/ckaznocha/intrange").
622-
WithNoopFallback(m.cfg, linter.IsGoLowerThan("1.22")),
622+
WithNoopFallback(m.cfg, linter.IsGoLowerThanGo122()),
623623

624624
linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
625625
WithSince("v1.43.0").

0 commit comments

Comments
 (0)