9
9
"go/ast"
10
10
"go/token"
11
11
"go/types"
12
- "strings"
13
12
14
13
"golang.org/x/tools/go/analysis"
15
14
"golang.org/x/tools/go/analysis/passes/inspect"
@@ -25,13 +24,15 @@ import (
25
24
// sort.Slice(s, func(i, j int) bool { return s[i] < s[j] })
26
25
// => slices.Sort(s)
27
26
//
28
- // It also supports the SliceStable variant .
27
+ // There is no slices.SortStable .
29
28
//
30
29
// TODO(adonovan): support
31
30
//
32
31
// - 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.
35
36
//
36
37
// - sort.Sort(x) where x has a named slice type whose Less method is the natural order.
37
38
// -> sort.Slice(x)
@@ -43,13 +44,11 @@ func sortslice(pass *analysis.Pass) {
43
44
info := pass .TypesInfo
44
45
45
46
check := func (file * ast.File , call * ast.CallExpr ) {
46
- // call to sort.Slice{,Stable} ?
47
+ // call to sort.Slice?
47
48
obj := typeutil .Callee (info , call )
48
- if ! analysisinternal .IsFunctionNamed (obj , "sort" , "Slice" , "SliceStable" ) {
49
+ if ! analysisinternal .IsFunctionNamed (obj , "sort" , "Slice" ) {
49
50
return
50
51
}
51
- stable := cond (strings .HasSuffix (obj .Name (), "Stable" ), "Stable" , "" )
52
-
53
52
if lit , ok := call .Args [1 ].(* ast.FuncLit ); ok && len (lit .Body .List ) == 1 {
54
53
sig := info .Types [lit .Type ].Type .(* types.Signature )
55
54
@@ -78,15 +77,15 @@ func sortslice(pass *analysis.Pass) {
78
77
Pos : call .Fun .Pos (),
79
78
End : call .Fun .End (),
80
79
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" ),
82
81
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" ),
84
83
TextEdits : append (importEdits , []analysis.TextEdit {
85
84
{
86
85
// Replace sort.Slice with slices.Sort.
87
86
Pos : call .Fun .Pos (),
88
87
End : call .Fun .End (),
89
- NewText : []byte (slicesName + ".Sort" + stable ),
88
+ NewText : []byte (slicesName + ".Sort" ),
90
89
},
91
90
{
92
91
// Eliminate FuncLit.
0 commit comments