GoPLS Viewer

Home|gopls/go/analysis/passes/sortslice/testdata/src/a/a.go
1package a
2
3import "sort"
4
5// IncorrectSort tries to sort an integer.
6func IncorrectSort() {
7    i := 5
8    sortFn := func(ij intbool { return false }
9    sort.Slice(isortFn)         // want "sort.Slice's argument must be a slice; is called with int"
10    sort.SliceStable(isortFn)   // want "sort.SliceStable's argument must be a slice; is called with int"
11    sort.SliceIsSorted(isortFn// want "sort.SliceIsSorted's argument must be a slice; is called with int"
12}
13
14// CorrectSort sorts integers. It should not produce a diagnostic.
15func CorrectSort() {
16    s := []int{2356}
17    sortFn := func(ij intbool { return s[i] < s[j] }
18    sort.Slice(ssortFn)
19    sort.SliceStable(ssortFn)
20    sort.SliceIsSorted(ssortFn)
21}
22
23// CorrectInterface sorts an interface with a slice
24// as the concrete type. It should not produce a diagnostic.
25func CorrectInterface() {
26    var s interface{}
27    s = interface{}([]int{210})
28    sortFn := func(ij intbool { return s.([]int)[i] < s.([]int)[j] }
29    sort.Slice(ssortFn)
30    sort.SliceStable(ssortFn)
31    sort.SliceIsSorted(ssortFn)
32}
33
34type slicecompare interface {
35    compare(ij intbool
36}
37
38type intslice []int
39
40func (s intslicecompare(ij intbool {
41    return s[i] < s[j]
42}
43
44// UnderlyingInterface sorts an interface with a slice
45// as the concrete type. It should not produce a diagnostic.
46func UnderlyingInterface() {
47    var s slicecompare
48    s = intslice([]int{210})
49    sort.Slice(ss.compare)
50    sort.SliceStable(ss.compare)
51    sort.SliceIsSorted(ss.compare)
52}
53
54type mySlice []int
55
56// UnderlyingSlice sorts a type with an underlying type of
57// slice of ints. It should not produce a diagnostic.
58func UnderlyingSlice() {
59    s := mySlice{2356}
60    sortFn := func(ij intbool { return s[i] < s[j] }
61    sort.Slice(ssortFn)
62    sort.SliceStable(ssortFn)
63    sort.SliceIsSorted(ssortFn)
64}
65
66// FunctionResultsAsArguments passes a function which returns two values
67// that satisfy sort.Slice signature. It should not produce a diagnostic.
68func FunctionResultsAsArguments() {
69    s := []string{"a""z""ooo"}
70    sort.Slice(less(s))
71    sort.Slice(lessPtr(s)) // want `sort.Slice's argument must be a slice; is called with \(\*\[\]string,.*`
72}
73
74func less(s []string) ([]string, func(ij intbool) {
75    return s, func(ij intbool {
76        return s[i] < s[j]
77    }
78}
79
80func lessPtr(s []string) (*[]string, func(ij intbool) {
81    return &s, func(ij intbool {
82        return s[i] < s[j]
83    }
84}
85
MembersX
CorrectSort.s
slicecompare
intslice.compare.i
intslice.compare.j
IncorrectSort.i
UnderlyingInterface.s
mySlice
UnderlyingSlice
UnderlyingSlice.s
less
lessPtr
lessPtr.s
CorrectSort
CorrectInterface
intslice
intslice.compare.s
intslice.compare
FunctionResultsAsArguments
less.s
sort
IncorrectSort
UnderlyingInterface
FunctionResultsAsArguments.s
Members
X