Skip to content

Commit 8de2a7f

Browse files
KimMachineGuntimothy-king
authored andcommitted
go/analysis/passes/sortslice: add missing functions using empty interface
Add missing functions using empty interface in `sort` package. Change-Id: Ic39bc55897705b08d3739a993b30d4b4e9a77baf GitHub-Last-Rev: 1cfb914 GitHub-Pull-Request: golang#318 Reviewed-on: https://go-review.googlesource.com/c/tools/+/319689 Reviewed-by: Tim King <[email protected]> Trust: Tim King <[email protected]> Trust: Than McIntosh <[email protected]> Run-TryBot: Tim King <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 9cd58b0 commit 8de2a7f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

go/analysis/passes/sortslice/analyzer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
4545
return
4646
}
4747

48-
if fn.FullName() != "sort.Slice" {
48+
fnName := fn.FullName()
49+
if fnName != "sort.Slice" && fnName != "sort.SliceStable" && fnName != "sort.SliceIsSorted" {
4950
return
5051
}
5152

@@ -115,7 +116,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
115116
pass.Report(analysis.Diagnostic{
116117
Pos: call.Pos(),
117118
End: call.End(),
118-
Message: fmt.Sprintf("sort.Slice's argument must be a slice; is called with %s", typ.String()),
119+
Message: fmt.Sprintf("%s's argument must be a slice; is called with %s", fnName, typ.String()),
119120
SuggestedFixes: fixes,
120121
})
121122
})

go/analysis/passes/sortslice/testdata/src/a/a.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ func IncorrectSort() {
77
i := 5
88
sortFn := func(i, j int) bool { return false }
99
sort.Slice(i, sortFn) // want "sort.Slice's argument must be a slice; is called with int"
10+
sort.SliceStable(i, sortFn) // want "sort.SliceStable's argument must be a slice; is called with int"
11+
sort.SliceIsSorted(i, sortFn) // want "sort.SliceIsSorted's argument must be a slice; is called with int"
1012
}
1113

1214
// CorrectSort sorts integers. It should not produce a diagnostic.
1315
func CorrectSort() {
1416
s := []int{2, 3, 5, 6}
1517
sortFn := func(i, j int) bool { return s[i] < s[j] }
1618
sort.Slice(s, sortFn)
19+
sort.SliceStable(s, sortFn)
20+
sort.SliceIsSorted(s, sortFn)
1721
}
1822

1923
// CorrectInterface sorts an interface with a slice
@@ -23,6 +27,8 @@ func CorrectInterface() {
2327
s = interface{}([]int{2, 1, 0})
2428
sortFn := func(i, j int) bool { return s.([]int)[i] < s.([]int)[j] }
2529
sort.Slice(s, sortFn)
30+
sort.SliceStable(s, sortFn)
31+
sort.SliceIsSorted(s, sortFn)
2632
}
2733

2834
type slicecompare interface {
@@ -41,6 +47,8 @@ func UnderlyingInterface() {
4147
var s slicecompare
4248
s = intslice([]int{2, 1, 0})
4349
sort.Slice(s, s.compare)
50+
sort.SliceStable(s, s.compare)
51+
sort.SliceIsSorted(s, s.compare)
4452
}
4553

4654
type mySlice []int
@@ -51,4 +59,6 @@ func UnderlyingSlice() {
5159
s := mySlice{2, 3, 5, 6}
5260
sortFn := func(i, j int) bool { return s[i] < s[j] }
5361
sort.Slice(s, sortFn)
62+
sort.SliceStable(s, sortFn)
63+
sort.SliceIsSorted(s, sortFn)
5464
}

0 commit comments

Comments
 (0)