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.
0 commit comments