Skip to content

Commit a9bf6fd

Browse files
committed
gopls/internal/analysis/modernize: remove SortStable
Remove the modernization from sort.SliceStable to slices.SortStable. There is no slices.SortStable. Change-Id: I55e6c6848aa2708976d35ceabab73e7b55da1d1f Reviewed-on: https://go-review.googlesource.com/c/tools/+/647735 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4d1de70 commit a9bf6fd

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

gopls/internal/analysis/modernize/sortslice.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"go/ast"
1010
"go/token"
1111
"go/types"
12-
"strings"
1312

1413
"golang.org/x/tools/go/analysis"
1514
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -25,13 +24,15 @@ import (
2524
// sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
2625
// => slices.Sort(s)
2726
//
28-
// It also supports the SliceStable variant.
27+
// There is no slices.SortStable.
2928
//
3029
// TODO(adonovan): support
3130
//
3231
// - sort.Slice(s, func(i, j int) bool { return s[i] ... s[j] })
33-
// -> slices.SortFunc(s, func(x, y int) bool { return x ... y })
34-
// iff all uses of i, j can be replaced by s[i], s[j].
32+
// -> slices.SortFunc(s, func(x, y T) int { return x ... y })
33+
// iff all uses of i, j can be replaced by s[i], s[j] and "<" can be replaced with cmp.Compare.
34+
//
35+
// - As above for sort.SliceStable -> slices.SortStableFunc.
3536
//
3637
// - sort.Sort(x) where x has a named slice type whose Less method is the natural order.
3738
// -> sort.Slice(x)
@@ -43,13 +44,11 @@ func sortslice(pass *analysis.Pass) {
4344
info := pass.TypesInfo
4445

4546
check := func(file *ast.File, call *ast.CallExpr) {
46-
// call to sort.Slice{,Stable}?
47+
// call to sort.Slice?
4748
obj := typeutil.Callee(info, call)
48-
if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice", "SliceStable") {
49+
if !analysisinternal.IsFunctionNamed(obj, "sort", "Slice") {
4950
return
5051
}
51-
stable := cond(strings.HasSuffix(obj.Name(), "Stable"), "Stable", "")
52-
5352
if lit, ok := call.Args[1].(*ast.FuncLit); ok && len(lit.Body.List) == 1 {
5453
sig := info.Types[lit.Type].Type.(*types.Signature)
5554

@@ -78,15 +77,15 @@ func sortslice(pass *analysis.Pass) {
7877
Pos: call.Fun.Pos(),
7978
End: call.Fun.End(),
8079
Category: "sortslice",
81-
Message: fmt.Sprintf("sort.Slice%[1]s can be modernized using slices.Sort%[1]s", stable),
80+
Message: fmt.Sprintf("sort.Slice can be modernized using slices.Sort"),
8281
SuggestedFixes: []analysis.SuggestedFix{{
83-
Message: fmt.Sprintf("Replace sort.Slice%[1]s call by slices.Sort%[1]s", stable),
82+
Message: fmt.Sprintf("Replace sort.Slice call by slices.Sort"),
8483
TextEdits: append(importEdits, []analysis.TextEdit{
8584
{
8685
// Replace sort.Slice with slices.Sort.
8786
Pos: call.Fun.Pos(),
8887
End: call.Fun.End(),
89-
NewText: []byte(slicesName + ".Sort" + stable),
88+
NewText: []byte(slicesName + ".Sort"),
9089
},
9190
{
9291
// Eliminate FuncLit.

gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ type myint int
66

77
func _(s []myint) {
88
sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort"
9-
10-
sort.SliceStable(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.SliceStable can be modernized using slices.SortStable"
119
}
1210

1311
func _(x *struct{ s []int }) {

gopls/internal/analysis/modernize/testdata/src/sortslice/sortslice.go.golden

-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import "slices"
44

55
import "slices"
66

7-
import "slices"
8-
97
import "sort"
108

119
type myint int
1210

1311
func _(s []myint) {
1412
slices.Sort(s) // want "sort.Slice can be modernized using slices.Sort"
15-
16-
slices.SortStable(s) // want "sort.SliceStable can be modernized using slices.SortStable"
1713
}
1814

1915
func _(x *struct{ s []int }) {

0 commit comments

Comments
 (0)