aboutsummaryrefslogtreecommitdiff
path: root/go/analysis/passes/sortslice/testdata/src/a/a.go
blob: bc6cc16e9f18d47d0588fe415d5f3a08c4a27acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package a

import "sort"

// IncorrectSort tries to sort an integer.
func IncorrectSort() {
	i := 5
	sortFn := func(i, j int) bool { return false }
	sort.Slice(i, sortFn) // want "sort.Slice's argument must be a slice; is called with int"
	sort.SliceStable(i, sortFn) // want "sort.SliceStable's argument must be a slice; is called with int"
	sort.SliceIsSorted(i, sortFn) // want "sort.SliceIsSorted's argument must be a slice; is called with int"
}

// CorrectSort sorts integers. It should not produce a diagnostic.
func CorrectSort() {
	s := []int{2, 3, 5, 6}
	sortFn := func(i, j int) bool { return s[i] < s[j] }
	sort.Slice(s, sortFn)
	sort.SliceStable(s, sortFn)
	sort.SliceIsSorted(s, sortFn)
}

// CorrectInterface sorts an interface with a slice
// as the concrete type. It should not produce a diagnostic.
func CorrectInterface() {
	var s interface{}
	s = interface{}([]int{2, 1, 0})
	sortFn := func(i, j int) bool { return s.([]int)[i] < s.([]int)[j] }
	sort.Slice(s, sortFn)
	sort.SliceStable(s, sortFn)
	sort.SliceIsSorted(s, sortFn)
}

type slicecompare interface {
	compare(i, j int) bool
}

type intslice []int

func (s intslice) compare(i, j int) bool {
	return s[i] < s[j]
}

// UnderlyingInterface sorts an interface with a slice
// as the concrete type. It should not produce a diagnostic.
func UnderlyingInterface() {
	var s slicecompare
	s = intslice([]int{2, 1, 0})
	sort.Slice(s, s.compare)
	sort.SliceStable(s, s.compare)
	sort.SliceIsSorted(s, s.compare)
}

type mySlice []int

// UnderlyingSlice sorts a type with an underlying type of
// slice of ints. It should not produce a diagnostic.
func UnderlyingSlice() {
	s := mySlice{2, 3, 5, 6}
	sortFn := func(i, j int) bool { return s[i] < s[j] }
	sort.Slice(s, sortFn)
	sort.SliceStable(s, sortFn)
	sort.SliceIsSorted(s, sortFn)
}