Skip to content

Commit 6e0c163

Browse files
committed
chore: replace golang.org/x/exp/maps by maps
1 parent e68d278 commit 6e0c163

File tree

13 files changed

+52
-66
lines changed

13 files changed

+52
-66
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ require (
126126
go-simpler.org/musttag v0.13.0
127127
go-simpler.org/sloglint v0.7.2
128128
go.uber.org/automaxprocs v1.6.0
129-
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
130129
golang.org/x/mod v0.22.0
131130
golang.org/x/sys v0.29.0
132131
golang.org/x/tools v0.28.0
@@ -196,6 +195,7 @@ require (
196195
go.uber.org/atomic v1.7.0 // indirect
197196
go.uber.org/multierr v1.6.0 // indirect
198197
go.uber.org/zap v1.24.0 // indirect
198+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
199199
golang.org/x/exp/typeparams v0.0.0-20241108190413-2d47ceb2692f // indirect
200200
golang.org/x/sync v0.10.0 // indirect
201201
golang.org/x/text v0.20.0 // indirect

internal/cache/cache.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"encoding/hex"
77
"errors"
88
"fmt"
9+
"maps"
910
"runtime"
1011
"slices"
1112
"strings"
1213
"sync"
1314

14-
"golang.org/x/exp/maps"
1515
"golang.org/x/tools/go/packages"
1616

1717
"github.com/golangci/golangci-lint/internal/go/cache"
@@ -178,9 +178,7 @@ func (c *Cache) computePkgHash(pkg *packages.Package) (hashResults, error) {
178178
curSum := key.Sum()
179179
hashRes[HashModeNeedOnlySelf] = hex.EncodeToString(curSum[:])
180180

181-
imps := maps.Values(pkg.Imports)
182-
183-
slices.SortFunc(imps, func(a, b *packages.Package) int {
181+
imps := slices.SortedFunc(maps.Values(pkg.Imports), func(a, b *packages.Package) int {
184182
return strings.Compare(a.PkgPath, b.PkgPath)
185183
})
186184

pkg/commands/run.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"fmt"
99
"io"
1010
"log"
11+
"maps"
1112
"os"
1213
"path/filepath"
1314
"runtime"
1415
"runtime/pprof"
1516
"runtime/trace"
16-
"sort"
17+
"slices"
1718
"strconv"
1819
"strings"
1920
"time"
@@ -24,7 +25,6 @@ import (
2425
"github.com/spf13/pflag"
2526
"github.com/spf13/viper"
2627
"go.uber.org/automaxprocs/maxprocs"
27-
"golang.org/x/exp/maps"
2828
"gopkg.in/yaml.v3"
2929

3030
"github.com/golangci/golangci-lint/internal/cache"
@@ -452,8 +452,7 @@ func (c *runCommand) printStats(issues []result.Issue) {
452452

453453
c.cmd.Printf("%d issues:\n", len(issues))
454454

455-
keys := maps.Keys(stats)
456-
sort.Strings(keys)
455+
keys := slices.Sorted(maps.Keys(stats))
457456

458457
for _, key := range keys {
459458
c.cmd.Printf("* %s: %d\n", key, stats[key])

pkg/goanalysis/runner.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"encoding/gob"
99
"fmt"
1010
"go/token"
11+
"maps"
1112
"runtime"
12-
"sort"
13+
"slices"
1314
"sync"
1415

15-
"golang.org/x/exp/maps"
1616
"golang.org/x/tools/go/analysis"
1717
"golang.org/x/tools/go/packages"
1818

@@ -159,8 +159,8 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac
159159
act.objectFacts = make(map[objectFactKey]analysis.Fact)
160160
act.packageFacts = make(map[packageFactKey]analysis.Fact)
161161

162-
paths := maps.Keys(pkg.Imports)
163-
sort.Strings(paths) // for determinism
162+
paths := slices.Sorted(maps.Keys(pkg.Imports)) // for determinism
163+
164164
for _, path := range paths {
165165
dep := r.makeAction(a, pkg.Imports[path], initialPkgs, actions, actAlloc)
166166
act.Deps = append(act.Deps, dep)
@@ -209,7 +209,7 @@ func (r *runner) prepareAnalysis(pkgs []*packages.Package,
209209
}
210210
}
211211

212-
allActions = maps.Values(actions)
212+
allActions = slices.Collect(maps.Values(actions))
213213

214214
debugf("Built %d actions", len(actions))
215215

pkg/golinters/gocritic/gocritic.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import (
55
"fmt"
66
"go/ast"
77
"go/types"
8+
"maps"
89
"reflect"
910
"runtime"
1011
"slices"
11-
"sort"
1212
"strings"
1313
"sync"
1414

1515
"github.com/go-critic/go-critic/checkers"
1616
gocriticlinter "github.com/go-critic/go-critic/linter"
1717
_ "github.com/quasilyte/go-ruleguard/dsl"
18-
"golang.org/x/exp/maps"
1918
"golang.org/x/tools/go/analysis"
2019

2120
"github.com/golangci/golangci-lint/pkg/config"
@@ -163,8 +162,7 @@ func (w *goCriticWrapper) configureCheckerInfo(
163162
info.Name, k)
164163
}
165164

166-
supportedKeys := maps.Keys(info.Params)
167-
sort.Strings(supportedKeys)
165+
supportedKeys := slices.Sorted(maps.Keys(info.Params))
168166

169167
return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
170168
info.Name, k, supportedKeys)
@@ -265,8 +263,7 @@ func newSettingsWrapper(settings *config.GoCriticSettings, logger logutils.Log)
265263
}
266264
}
267265

268-
allTagsSorted := maps.Keys(allChecksByTag)
269-
sort.Strings(allTagsSorted)
266+
allTagsSorted := slices.Sorted(maps.Keys(allChecksByTag))
270267

271268
return &settingsWrapper{
272269
GoCriticSettings: settings,
@@ -553,6 +550,5 @@ func debugChecksListf(checks []string, format string, args ...any) {
553550
}
554551

555552
func sprintSortedStrings(v []string) string {
556-
sort.Strings(slices.Clone(v))
557-
return fmt.Sprint(v)
553+
return fmt.Sprint(slices.Sorted(slices.Values(v)))
558554
}

pkg/golinters/gocritic/gocritic_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gocritic
22

33
import (
4+
"maps"
45
"slices"
56
"strings"
67
"testing"
@@ -9,7 +10,6 @@ import (
910
gocriticlinter "github.com/go-critic/go-critic/linter"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
12-
"golang.org/x/exp/maps"
1313

1414
"github.com/golangci/golangci-lint/pkg/config"
1515
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -40,7 +40,7 @@ func Test_settingsWrapper_InferEnabledChecks(t *testing.T) {
4040
t.Logf("enabled by default checks:\n%s", strings.Join(enabledByDefaultChecks, "\n"))
4141

4242
insert := func(in []string, toInsert ...string) []string {
43-
return append(slices.Clone(in), toInsert...)
43+
return slices.Concat(in, toInsert)
4444
}
4545

4646
remove := func(in []string, toRemove ...string) []string {
@@ -54,9 +54,7 @@ func Test_settingsWrapper_InferEnabledChecks(t *testing.T) {
5454
}
5555

5656
uniq := func(in []string) []string {
57-
result := slices.Clone(in)
58-
slices.Sort(result)
59-
return slices.Compact(result)
57+
return slices.Compact(slices.Sorted(slices.Values(in)))
6058
}
6159

6260
cases := []struct {
@@ -269,7 +267,7 @@ func Test_settingsWrapper_InferEnabledChecks(t *testing.T) {
269267
wr := newSettingsWrapper(tt.sett, lg)
270268

271269
wr.InferEnabledChecks()
272-
assert.ElementsMatch(t, tt.expectedEnabledChecks, maps.Keys(wr.inferredEnabledChecks))
270+
assert.ElementsMatch(t, tt.expectedEnabledChecks, slices.Collect(maps.Keys(wr.inferredEnabledChecks)))
273271
assert.NoError(t, wr.Validate())
274272
})
275273
}

pkg/golinters/thelper/thelper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package thelper
22

33
import (
4+
"maps"
5+
"slices"
46
"strings"
57

68
"github.com/kulti/thelper/pkg/analyzer"
7-
"golang.org/x/exp/maps"
89
"golang.org/x/tools/go/analysis"
910

1011
"github.com/golangci/golangci-lint/pkg/config"
@@ -44,7 +45,7 @@ func New(settings *config.ThelperSettings) *goanalysis.Linter {
4445
internal.LinterLogger.Fatalf("thelper: at least one option must be enabled")
4546
}
4647

47-
args := maps.Keys(opts)
48+
args := slices.Collect(maps.Keys(opts))
4849

4950
cfg := map[string]map[string]any{
5051
a.Name: {

pkg/lint/lintersdb/manager.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package lintersdb
22

33
import (
44
"fmt"
5+
"maps"
56
"os"
67
"slices"
78
"sort"
8-
9-
"golang.org/x/exp/maps"
9+
"strings"
1010

1111
"github.com/golangci/golangci-lint/pkg/config"
1212
"github.com/golangci/golangci-lint/pkg/goanalysis"
@@ -109,24 +109,25 @@ func (m *Manager) GetOptimizedLinters() ([]*linter.Config, error) {
109109

110110
m.combineGoAnalysisLinters(resultLintersSet)
111111

112-
resultLinters := maps.Values(resultLintersSet)
113-
114112
// Make order of execution of linters (go/analysis metalinter and unused) stable.
115-
sort.Slice(resultLinters, func(i, j int) bool {
116-
a, b := resultLinters[i], resultLinters[j]
117-
113+
resultLinters := slices.SortedFunc(maps.Values(resultLintersSet), func(a *linter.Config, b *linter.Config) int {
118114
if b.Name() == linter.LastLinter {
119-
return true
115+
return -1
120116
}
121117

122118
if a.Name() == linter.LastLinter {
123-
return false
119+
return 1
124120
}
125121

126122
if a.DoesChangeTypes != b.DoesChangeTypes {
127-
return b.DoesChangeTypes // move type-changing linters to the end to optimize speed
123+
// move type-changing linters to the end to optimize speed
124+
if b.DoesChangeTypes {
125+
return -1
126+
}
127+
return 1
128128
}
129-
return a.Name() < b.Name()
129+
130+
return strings.Compare(a.Name(), b.Name())
130131
})
131132

132133
return resultLinters, nil

pkg/printers/checkstyle.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"encoding/xml"
55
"fmt"
66
"io"
7-
"sort"
7+
"maps"
8+
"slices"
9+
"strings"
810

911
"github.com/go-xmlfmt/xmlfmt"
10-
"golang.org/x/exp/maps"
1112

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

78-
out.Files = maps.Values(files)
79-
80-
sort.Slice(out.Files, func(i, j int) bool {
81-
return out.Files[i].Name < out.Files[j].Name
79+
out.Files = slices.SortedFunc(maps.Values(files), func(a *checkstyleFile, b *checkstyleFile) int {
80+
return strings.Compare(a.Name, b.Name)
8281
})
8382

8483
data, err := xml.Marshal(&out)

pkg/printers/junitxml.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"encoding/xml"
55
"fmt"
66
"io"
7-
"sort"
7+
"maps"
8+
"slices"
89
"strings"
910

10-
"golang.org/x/exp/maps"
11-
1211
"github.com/golangci/golangci-lint/pkg/result"
1312
)
1413

@@ -84,10 +83,9 @@ func (p JunitXML) Print(issues []result.Issue) error {
8483
}
8584

8685
var res testSuitesXML
87-
res.TestSuites = maps.Values(suites)
8886

89-
sort.Slice(res.TestSuites, func(i, j int) bool {
90-
return res.TestSuites[i].Suite < res.TestSuites[j].Suite
87+
res.TestSuites = slices.SortedFunc(maps.Values(suites), func(a testSuiteXML, b testSuiteXML) int {
88+
return strings.Compare(a.Suite, b.Suite)
9189
})
9290

9391
enc := xml.NewEncoder(p.w)

pkg/result/processors/fixer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ package processors
99
import (
1010
"errors"
1111
"fmt"
12+
"maps"
1213
"os"
1314
"slices"
1415

15-
"golang.org/x/exp/maps"
16-
1716
"github.com/golangci/golangci-lint/internal/x/tools/diff"
1817
"github.com/golangci/golangci-lint/pkg/config"
1918
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -124,7 +123,7 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
124123
for path, linterToEdits := range editsByLinter {
125124
excludedLinters := make(map[string]struct{})
126125

127-
linters := maps.Keys(linterToEdits)
126+
linters := slices.Collect(maps.Keys(linterToEdits))
128127

129128
// Does any linter create conflicting edits?
130129
for _, linter := range linters {

pkg/result/processors/nolint.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"go/ast"
55
"go/parser"
66
"go/token"
7+
"maps"
78
"regexp"
9+
"slices"
810
"sort"
911
"strings"
1012

11-
"golang.org/x/exp/maps"
12-
1313
"github.com/golangci/golangci-lint/pkg/golinters/nolintlint"
1414
"github.com/golangci/golangci-lint/pkg/lint/linter"
1515
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
@@ -101,8 +101,7 @@ func (p *Nolint) Finish() {
101101
return
102102
}
103103

104-
unknownLinters := maps.Keys(p.unknownLintersSet)
105-
sort.Strings(unknownLinters)
104+
unknownLinters := slices.Sorted(maps.Keys(p.unknownLintersSet))
106105

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

scripts/website/expand_templates/thanks.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package main
22

33
import (
44
"fmt"
5-
"sort"
5+
"maps"
6+
"slices"
67
"strings"
78

8-
"golang.org/x/exp/maps"
9-
109
"github.com/golangci/golangci-lint/pkg/config"
1110
"github.com/golangci/golangci-lint/pkg/lint/linter"
1211
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
@@ -69,9 +68,8 @@ func getThanksList() string {
6968
}
7069
}
7170

72-
authors := maps.Keys(addedAuthors)
73-
sort.Slice(authors, func(i, j int) bool {
74-
return strings.ToLower(authors[i]) < strings.ToLower(authors[j])
71+
authors := slices.SortedFunc(maps.Keys(addedAuthors), func(a string, b string) int {
72+
return strings.Compare(strings.ToLower(a), strings.ToLower(b))
7573
})
7674

7775
lines := []string{

0 commit comments

Comments
 (0)