Debian Codegraph @

go/analysis/passes/modernize/testdata/src/slicessort/slicessort.go

raw
1 package slicessort
2
3 import "sort"
4
5 type myint int
6
7 func _(s []myint) {
8 sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) // want "sort.Slice can be modernized using slices.Sort"
9 }
10
11 func _(x *struct{ s []int }) {
12 sort.Slice(x.s, func(first, second int) bool { return x.s[first] < x.s[second] }) // want "sort.Slice can be modernized using slices.Sort"
13 }
14
15 func _(s []int) {
16 sort.Slice(s, func(i, j int) bool { return s[i] > s[j] }) // nope: wrong comparison operator
17 }
18
19 func _(s []int) {
20 sort.Slice(s, func(i, j int) bool { return s[j] < s[i] }) // nope: wrong index var
21 }
22
23 func _(sense bool, s2 []struct{ x int }) {
24 sort.Slice(s2, func(i, j int) bool { return s2[i].x < s2[j].x }) // nope: not a simple index operation
25
26 // Regression test for a crash: the sole statement of a
27 // comparison func body is not necessarily a return!
28 sort.Slice(s2, func(i, j int) bool {
29 if sense {
30 return s2[i].x < s2[j].x
31 } else {
32 return s2[i].x > s2[j].x
33 }
34 })
35 }