github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/analysis/passes/sortslice/testdata/src/a/a.go (about) 1 package a 2 3 import "sort" 4 5 // IncorrectSort tries to sort an integer. 6 func IncorrectSort() { 7 i := 5 8 sortFn := func(i, j int) bool { return false } 9 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" 12 } 13 14 // CorrectSort sorts integers. It should not produce a diagnostic. 15 func CorrectSort() { 16 s := []int{2, 3, 5, 6} 17 sortFn := func(i, j int) bool { return s[i] < s[j] } 18 sort.Slice(s, sortFn) 19 sort.SliceStable(s, sortFn) 20 sort.SliceIsSorted(s, sortFn) 21 } 22 23 // CorrectInterface sorts an interface with a slice 24 // as the concrete type. It should not produce a diagnostic. 25 func CorrectInterface() { 26 var s interface{} 27 s = interface{}([]int{2, 1, 0}) 28 sortFn := func(i, j int) bool { return s.([]int)[i] < s.([]int)[j] } 29 sort.Slice(s, sortFn) 30 sort.SliceStable(s, sortFn) 31 sort.SliceIsSorted(s, sortFn) 32 } 33 34 type slicecompare interface { 35 compare(i, j int) bool 36 } 37 38 type intslice []int 39 40 func (s intslice) compare(i, j int) bool { 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. 46 func UnderlyingInterface() { 47 var s slicecompare 48 s = intslice([]int{2, 1, 0}) 49 sort.Slice(s, s.compare) 50 sort.SliceStable(s, s.compare) 51 sort.SliceIsSorted(s, s.compare) 52 } 53 54 type mySlice []int 55 56 // UnderlyingSlice sorts a type with an underlying type of 57 // slice of ints. It should not produce a diagnostic. 58 func UnderlyingSlice() { 59 s := mySlice{2, 3, 5, 6} 60 sortFn := func(i, j int) bool { return s[i] < s[j] } 61 sort.Slice(s, sortFn) 62 sort.SliceStable(s, sortFn) 63 sort.SliceIsSorted(s, sortFn) 64 }