Skip to content

Commit 521a676

Browse files
alexandearldez
andauthored
dev: replace raw loops with funcs from slices and maps (#4299)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent d23c354 commit 521a676

File tree

11 files changed

+44
-70
lines changed

11 files changed

+44
-70
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ require (
122122
gitlab.com/bosi/decorder v0.4.1
123123
go-simpler.org/musttag v0.8.0
124124
go-simpler.org/sloglint v0.4.0
125-
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
125+
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
126126
golang.org/x/tools v0.16.1
127127
gopkg.in/yaml.v3 v3.0.1
128128
honnef.co/go/tools v0.4.6

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/goanalysis/runner.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sort"
1818
"sync"
1919

20+
"golang.org/x/exp/maps"
2021
"golang.org/x/tools/go/analysis"
2122
"golang.org/x/tools/go/packages"
2223

@@ -159,10 +160,7 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac
159160
act.objectFacts = make(map[objectFactKey]analysis.Fact)
160161
act.packageFacts = make(map[packageFactKey]analysis.Fact)
161162

162-
paths := make([]string, 0, len(pkg.Imports))
163-
for path := range pkg.Imports {
164-
paths = append(paths, path)
165-
}
163+
paths := maps.Keys(pkg.Imports)
166164
sort.Strings(paths) // for determinism
167165
for _, path := range paths {
168166
dep := r.makeAction(a, pkg.Imports[path], initialPkgs, actions, actAlloc)
@@ -212,10 +210,7 @@ func (r *runner) prepareAnalysis(pkgs []*packages.Package,
212210
}
213211
}
214212

215-
allActions := make([]*action, 0, len(actions))
216-
for _, act := range actions {
217-
allActions = append(allActions, act)
218-
}
213+
allActions := maps.Values(actions)
219214

220215
debugf("Built %d actions", len(actions))
221216

pkg/golinters/gocritic.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/go-critic/go-critic/checkers"
1616
gocriticlinter "github.com/go-critic/go-critic/linter"
17+
"golang.org/x/exp/maps"
1718
"golang.org/x/tools/go/analysis"
1819

1920
"github.com/golangci/golangci-lint/pkg/config"
@@ -219,10 +220,7 @@ func (w *goCriticWrapper) configureCheckerInfo(info *gocriticlinter.CheckerInfo,
219220
info.Name, k)
220221
}
221222

222-
var supportedKeys []string
223-
for sk := range info.Params {
224-
supportedKeys = append(supportedKeys, sk)
225-
}
223+
supportedKeys := maps.Keys(info.Params)
226224
sort.Strings(supportedKeys)
227225

228226
return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
@@ -311,11 +309,7 @@ func (s *goCriticSettingsWrapper) checkerTagsDebugf() {
311309

312310
tagToCheckers := s.buildTagToCheckersMap()
313311

314-
allTags := make([]string, 0, len(tagToCheckers))
315-
for tag := range tagToCheckers {
316-
allTags = append(allTags, tag)
317-
}
318-
312+
allTags := maps.Keys(tagToCheckers)
319313
sort.Strings(allTags)
320314

321315
goCriticDebugf("All gocritic existing tags and checks:")
@@ -609,12 +603,7 @@ func intersectStringSlice(s1, s2 []string) []string {
609603
}
610604

611605
func sprintAllowedCheckerNames(allowedNames map[string]bool) string {
612-
namesSlice := make([]string, 0, len(allowedNames))
613-
614-
for name := range allowedNames {
615-
namesSlice = append(namesSlice, name)
616-
}
617-
606+
namesSlice := maps.Keys(allowedNames)
618607
return sprintStrings(namesSlice)
619608
}
620609

pkg/golinters/govet_test.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package golinters
22

33
import (
4-
"sort"
54
"testing"
65

6+
"golang.org/x/exp/slices"
77
"golang.org/x/tools/go/analysis"
88
"golang.org/x/tools/go/analysis/passes/asmdecl"
99
"golang.org/x/tools/go/analysis/passes/assign"
@@ -18,39 +18,41 @@ import (
1818

1919
func TestGovet(t *testing.T) {
2020
// Checking that every default analyzer is in "all analyzers" list.
21-
var checkList []*analysis.Analyzer
22-
checkList = append(checkList, defaultAnalyzers...)
21+
checkList := append([]*analysis.Analyzer{}, defaultAnalyzers...)
2322
checkList = append(checkList, shadow.Analyzer) // special case, used in analyzersFromConfig
2423

2524
for _, defaultAnalyzer := range checkList {
26-
found := false
27-
for _, a := range allAnalyzers {
28-
if a.Name == defaultAnalyzer.Name {
29-
found = true
30-
break
31-
}
32-
}
25+
found := slices.ContainsFunc(allAnalyzers, func(a *analysis.Analyzer) bool {
26+
return a.Name == defaultAnalyzer.Name
27+
})
3328
if !found {
3429
t.Errorf("%s is not in allAnalyzers", defaultAnalyzer.Name)
3530
}
3631
}
3732
}
3833

39-
type sortedAnalyzers []*analysis.Analyzer
34+
func sortAnalyzers(a, b *analysis.Analyzer) int {
35+
if a.Name < b.Name {
36+
return -1
37+
}
38+
39+
if a.Name > b.Name {
40+
return 1
41+
}
4042

41-
func (p sortedAnalyzers) Len() int { return len(p) }
42-
func (p sortedAnalyzers) Less(i, j int) bool { return p[i].Name < p[j].Name }
43-
func (p sortedAnalyzers) Swap(i, j int) { p[i].Name, p[j].Name = p[j].Name, p[i].Name }
43+
return 0
44+
}
4445

4546
func TestGovetSorted(t *testing.T) {
4647
// Keeping analyzers sorted so their order match the import order.
4748
t.Run("All", func(t *testing.T) {
48-
if !sort.IsSorted(sortedAnalyzers(allAnalyzers)) {
49+
if !slices.IsSortedFunc(allAnalyzers, sortAnalyzers) {
4950
t.Error("please keep all analyzers list sorted by name")
5051
}
5152
})
53+
5254
t.Run("Default", func(t *testing.T) {
53-
if !sort.IsSorted(sortedAnalyzers(defaultAnalyzers)) {
55+
if !slices.IsSortedFunc(defaultAnalyzers, sortAnalyzers) {
5456
t.Error("please keep default analyzers list sorted by name")
5557
}
5658
})

pkg/golinters/thelper.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55

66
"github.com/kulti/thelper/pkg/analyzer"
7+
"golang.org/x/exp/maps"
78
"golang.org/x/tools/go/analysis"
89

910
"github.com/golangci/golangci-lint/pkg/config"
@@ -42,10 +43,7 @@ func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
4243
linterLogger.Fatalf("thelper: at least one option must be enabled")
4344
}
4445

45-
var args []string
46-
for k := range opts {
47-
args = append(args, k)
48-
}
46+
args := maps.Keys(opts)
4947

5048
cfgMap := map[string]map[string]any{
5149
a.Name: {

pkg/lint/lintersdb/enabled_set.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"os"
55
"sort"
66

7+
"golang.org/x/exp/maps"
8+
79
"github.com/golangci/golangci-lint/pkg/config"
810
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
911
"github.com/golangci/golangci-lint/pkg/lint/linter"
@@ -115,10 +117,7 @@ func (es EnabledSet) GetOptimizedLinters() ([]*linter.Config, error) {
115117
es.verbosePrintLintersStatus(resultLintersSet)
116118
es.combineGoAnalysisLinters(resultLintersSet)
117119

118-
var resultLinters []*linter.Config
119-
for _, lc := range resultLintersSet {
120-
resultLinters = append(resultLinters, lc)
121-
}
120+
resultLinters := maps.Values(resultLintersSet)
122121

123122
// Make order of execution of linters (go/analysis metalinter and unused) stable.
124123
sort.Slice(resultLinters, func(i, j int) bool {
@@ -185,10 +184,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
185184

186185
ml := goanalysis.NewMetaLinter(goanalysisLinters)
187186

188-
var presets []string
189-
for p := range goanalysisPresets {
190-
presets = append(presets, p)
191-
}
187+
presets := maps.Keys(goanalysisPresets)
192188

193189
mlConfig := &linter.Config{
194190
Linter: ml,

pkg/printers/checkstyle.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88

99
"github.com/go-xmlfmt/xmlfmt"
10+
"golang.org/x/exp/maps"
1011

1112
"github.com/golangci/golangci-lint/pkg/result"
1213
)
@@ -74,10 +75,7 @@ func (p Checkstyle) Print(issues []result.Issue) error {
7475
file.Errors = append(file.Errors, newError)
7576
}
7677

77-
out.Files = make([]*checkstyleFile, 0, len(files))
78-
for _, file := range files {
79-
out.Files = append(out.Files, file)
80-
}
78+
out.Files = maps.Values(files)
8179

8280
sort.Slice(out.Files, func(i, j int) bool {
8381
return out.Files[i].Name < out.Files[j].Name

pkg/printers/junitxml.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"sort"
88
"strings"
99

10+
"golang.org/x/exp/maps"
11+
1012
"github.com/golangci/golangci-lint/pkg/result"
1113
)
1214

@@ -71,9 +73,7 @@ func (p JunitXML) Print(issues []result.Issue) error {
7173
}
7274

7375
var res testSuitesXML
74-
for _, val := range suites {
75-
res.TestSuites = append(res.TestSuites, val)
76-
}
76+
res.TestSuites = maps.Values(suites)
7777

7878
sort.Slice(res.TestSuites, func(i, j int) bool {
7979
return res.TestSuites[i].Suite < res.TestSuites[j].Suite

pkg/result/processors/nolint.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"sort"
1010
"strings"
1111

12+
"golang.org/x/exp/maps"
13+
1214
"github.com/golangci/golangci-lint/pkg/golinters"
1315
"github.com/golangci/golangci-lint/pkg/lint/linter"
1416
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
@@ -289,10 +291,7 @@ func (p *Nolint) Finish() {
289291
return
290292
}
291293

292-
unknownLinters := make([]string, 0, len(p.unknownLintersSet))
293-
for name := range p.unknownLintersSet {
294-
unknownLinters = append(unknownLinters, name)
295-
}
294+
unknownLinters := maps.Keys(p.unknownLintersSet)
296295
sort.Strings(unknownLinters)
297296

298297
p.log.Warnf("Found unknown linters in //nolint directives: %s", strings.Join(unknownLinters, ", "))

scripts/expand_website_templates/main.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"unicode"
1717
"unicode/utf8"
1818

19+
"golang.org/x/exp/maps"
1920
"gopkg.in/yaml.v3"
2021

2122
"github.com/golangci/golangci-lint/internal/renameio"
@@ -360,11 +361,7 @@ func getThanksList() string {
360361
}
361362
}
362363

363-
var authors []string
364-
for author := range addedAuthors {
365-
authors = append(authors, author)
366-
}
367-
364+
authors := maps.Keys(addedAuthors)
368365
sort.Slice(authors, func(i, j int) bool {
369366
return strings.ToLower(authors[i]) < strings.ToLower(authors[j])
370367
})

0 commit comments

Comments
 (0)