Skip to content

Commit af68675

Browse files
committed
chore: replace golang.org/x/exp/maps by maps
1 parent dafd655 commit af68675

File tree

12 files changed

+50
-63
lines changed

12 files changed

+50
-63
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.28.0
132131
golang.org/x/tools v0.28.0
@@ -195,6 +194,7 @@ require (
195194
go.uber.org/atomic v1.7.0 // indirect
196195
go.uber.org/multierr v1.6.0 // indirect
197196
go.uber.org/zap v1.24.0 // indirect
197+
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
198198
golang.org/x/exp/typeparams v0.0.0-20241108190413-2d47ceb2692f // indirect
199199
golang.org/x/sync v0.10.0 // indirect
200200
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"
@@ -445,8 +445,7 @@ func (c *runCommand) printStats(issues []result.Issue) {
445445

446446
c.cmd.Printf("%d issues:\n", len(issues))
447447

448-
keys := maps.Keys(stats)
449-
sort.Strings(keys)
448+
keys := slices.Sorted(maps.Keys(stats))
450449

451450
for _, key := range keys {
452451
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

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

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

211-
allActions = maps.Values(actions)
211+
allActions = slices.Collect(maps.Values(actions))
212212

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

pkg/golinters/gocritic/gocritic.go

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

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

2221
"github.com/golangci/golangci-lint/pkg/config"
@@ -184,8 +183,7 @@ func (w *goCriticWrapper) configureCheckerInfo(
184183
info.Name, k)
185184
}
186185

187-
supportedKeys := maps.Keys(info.Params)
188-
sort.Strings(supportedKeys)
186+
supportedKeys := slices.Sorted(maps.Keys(info.Params))
189187

190188
return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
191189
info.Name, k, supportedKeys)
@@ -297,8 +295,7 @@ func newSettingsWrapper(settings *config.GoCriticSettings, logger logutils.Log)
297295
}
298296
}
299297

300-
allTagsSorted := maps.Keys(allChecksByTag)
301-
sort.Strings(allTagsSorted)
298+
allTagsSorted := slices.Sorted(maps.Keys(allChecksByTag))
302299

303300
return &settingsWrapper{
304301
GoCriticSettings: settings,
@@ -585,6 +582,5 @@ func debugChecksListf(checks []string, format string, args ...any) {
585582
}
586583

587584
func sprintSortedStrings(v []string) string {
588-
sort.Strings(slices.Clone(v))
589-
return fmt.Sprint(v)
585+
return fmt.Sprint(slices.Sorted(slices.Values(v)))
590586
}

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)

0 commit comments

Comments
 (0)