Skip to content

Commit 0a78585

Browse files
committed
feat: add modernize suite
1 parent 870ddc1 commit 0a78585

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed

.golangci.next.reference.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ linters:
8585
- mirror
8686
- misspell
8787
- mnd
88+
- modernize
8889
- musttag
8990
- nakedret
9091
- nestif
@@ -199,6 +200,7 @@ linters:
199200
- mirror
200201
- misspell
201202
- mnd
203+
- modernize
202204
- musttag
203205
- nakedret
204206
- nestif
@@ -2106,6 +2108,45 @@ linters:
21062108
- '^math\.'
21072109
- '^http\.StatusText$'
21082110

2111+
modernize:
2112+
disable:
2113+
# Replace interface{} with any.
2114+
- any
2115+
# Replace for-range over b.N with b.Loop.
2116+
- bloop
2117+
# Replace []byte(fmt.Sprintf) with fmt.Appendf.
2118+
- fmtappendf
2119+
# Remove redundant re-declaration of loop variables.
2120+
- forvar
2121+
# Replace explicit loops over maps with calls to maps package.
2122+
- mapsloop
2123+
# Replace if/else statements with calls to min or max.
2124+
- minmax
2125+
# Simplify code by using go1.26's new(expr).
2126+
- newexpr
2127+
# Suggest replacing omitempty with omitzero for struct fields.
2128+
- omitzero
2129+
# Replace 3-clause for loops with for-range over integers.
2130+
- rangeint
2131+
# Replace reflect.TypeOf(x) with TypeFor[T]().
2132+
- reflecttypefor
2133+
# Replace loops with slices.Contains or slices.ContainsFunc.
2134+
- slicescontains
2135+
# Replace sort.Slice with slices.Sort for basic types.
2136+
- slicessort
2137+
# Use iterators instead of Len/At-style APIs.
2138+
- stditerators
2139+
# Replace HasPrefix/TrimPrefix with CutPrefix.
2140+
- stringscutprefix
2141+
# Replace ranging over Split/Fields with SplitSeq/FieldsSeq.
2142+
- stringsseq
2143+
# Replace += with strings.Builder.
2144+
- stringsbuilder
2145+
# Replace context.WithCancel with t.Context in tests.
2146+
- testingcontext
2147+
# Replace wg.Add(1)/go/wg.Done() with wg.Go.
2148+
- waitgroup
2149+
21092150
musttag:
21102151
# A set of custom functions to check in addition to the builtin ones.
21112152
# Default: json, xml, gopkg.in/yaml.v3, BurntSushi/toml, mitchellh/mapstructure, jmoiron/sqlx

jsonschema/golangci.next.jsonschema.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,28 @@
706706
"header"
707707
]
708708
},
709+
"modernize-analyzers": {
710+
"enum": [
711+
"any",
712+
"bloop",
713+
"fmtappendf",
714+
"forvar",
715+
"mapsloop",
716+
"minmax",
717+
"newexpr",
718+
"omitzero",
719+
"rangeint",
720+
"reflecttypefor",
721+
"slicescontains",
722+
"slicessort",
723+
"stditerators",
724+
"stringscutprefix",
725+
"stringsseq",
726+
"stringsbuilder",
727+
"testingcontext",
728+
"waitgroup"
729+
]
730+
},
709731
"wsl-checks": {
710732
"enum": [
711733
"assign",
@@ -835,6 +857,7 @@
835857
"mirror",
836858
"misspell",
837859
"mnd",
860+
"modernize",
838861
"musttag",
839862
"nakedret",
840863
"nestif",
@@ -2829,6 +2852,19 @@
28292852
}
28302853
}
28312854
},
2855+
"modernizeSettings": {
2856+
"type": "object",
2857+
"additionalProperties": false,
2858+
"properties": {
2859+
"disable": {
2860+
"description": "List of analyzers to disable.",
2861+
"type": "array",
2862+
"items": {
2863+
"$ref": "#/definitions/modernize-analyzers"
2864+
}
2865+
}
2866+
}
2867+
},
28322868
"nolintlintSettings": {
28332869
"type": "object",
28342870
"additionalProperties": false,
@@ -4747,6 +4783,9 @@
47474783
"mnd": {
47484784
"$ref": "#/definitions/settings/definitions/mndSettings"
47494785
},
4786+
"modernize": {
4787+
"$ref": "#/definitions/settings/definitions/modernizeSettings"
4788+
},
47504789
"nolintlint":{
47514790
"$ref": "#/definitions/settings/definitions/nolintlintSettings"
47524791
},

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ type LintersSettings struct {
269269
Makezero MakezeroSettings `mapstructure:"makezero"`
270270
Misspell MisspellSettings `mapstructure:"misspell"`
271271
Mnd MndSettings `mapstructure:"mnd"`
272+
Modernize ModernizeSettings `mapstructure:"modernize"`
272273
MustTag MustTagSettings `mapstructure:"musttag"`
273274
Nakedret NakedretSettings `mapstructure:"nakedret"`
274275
Nestif NestifSettings `mapstructure:"nestif"`
@@ -758,6 +759,10 @@ type MndSettings struct {
758759
IgnoredFunctions []string `mapstructure:"ignored-functions"`
759760
}
760761

762+
type ModernizeSettings struct {
763+
Disable []string `mapstructure:"disable"`
764+
}
765+
761766
type NoLintLintSettings struct {
762767
RequireExplanation bool `mapstructure:"require-explanation"`
763768
RequireSpecific bool `mapstructure:"require-specific"`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package modernize
2+
3+
import (
4+
"slices"
5+
6+
"github.com/golangci/golangci-lint/v2/pkg/config"
7+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
8+
9+
"golang.org/x/tools/go/analysis"
10+
"golang.org/x/tools/go/analysis/passes/modernize"
11+
)
12+
13+
func New(settings *config.ModernizeSettings) *goanalysis.Linter {
14+
var analyzers []*analysis.Analyzer
15+
16+
if settings == nil {
17+
analyzers = modernize.Suite
18+
} else {
19+
for _, analyzer := range modernize.Suite {
20+
if slices.Contains(settings.Disable, analyzer.Name) {
21+
continue
22+
}
23+
24+
analyzers = append(analyzers, analyzer)
25+
}
26+
}
27+
28+
return goanalysis.NewLinter(
29+
"modernize",
30+
"A suite of analyzers that suggest simplifications to Go code, using modern language and library features.",
31+
analyzers,
32+
nil).
33+
WithLoadMode(goanalysis.LoadModeTypesInfo)
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package modernize
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
12+
13+
func TestFix(t *testing.T) {
14+
integration.RunFix(t)
15+
}
16+
17+
func TestFixPathPrefix(t *testing.T) {
18+
integration.RunFixPathPrefix(t)
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//golangcitest:args -Emodernize
2+
package testdata
3+
4+
func _(x interface{}) {} // want "interface{} can be replaced by any"
5+
6+
func _() {
7+
var x interface{} // want "interface{} can be replaced by any"
8+
const any = 1
9+
var y interface{} // nope: any is shadowed here
10+
_, _ = x, y
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//golangcitest:args -Emodernize
2+
//golangcitest:config_path testdata/modernize_custom.yml
3+
//golangcitest:expected_exitcode 0
4+
package testdata
5+
6+
func _(x interface{}) {}
7+
8+
func _() {
9+
var x interface{}
10+
const any = 1
11+
var y interface{} // nope: any is shadowed here
12+
_, _ = x, y
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
modernize:
6+
disable:
7+
- any

pkg/lint/lintersdb/builder_linter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import (
7373
"github.com/golangci/golangci-lint/v2/pkg/golinters/mirror"
7474
"github.com/golangci/golangci-lint/v2/pkg/golinters/misspell"
7575
"github.com/golangci/golangci-lint/v2/pkg/golinters/mnd"
76+
"github.com/golangci/golangci-lint/v2/pkg/golinters/modernize"
7677
"github.com/golangci/golangci-lint/v2/pkg/golinters/musttag"
7778
"github.com/golangci/golangci-lint/v2/pkg/golinters/nakedret"
7879
"github.com/golangci/golangci-lint/v2/pkg/golinters/nestif"
@@ -382,6 +383,11 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
382383
WithSince("v1.22.0").
383384
WithURL("https://github.com/tommy-muehle/go-mnd"),
384385

386+
linter.NewConfig(modernize.New(&cfg.Linters.Settings.Modernize)).
387+
WithSince("v2.6.0").
388+
WithLoadForGoAnalysis().
389+
WithURL("https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize"),
390+
385391
linter.NewConfig(gomoddirectives.New(&cfg.Linters.Settings.GoModDirectives)).
386392
WithSince("v1.39.0").
387393
WithURL("https://github.com/ldez/gomoddirectives"),

0 commit comments

Comments
 (0)