Skip to content

Commit

Permalink
go/analysis/passes/sortslice: add missing functions using empty inter…
Browse files Browse the repository at this point in the history
…face

Add missing functions using empty interface in `sort` package.

Change-Id: Ic39bc55897705b08d3739a993b30d4b4e9a77baf
GitHub-Last-Rev: 1cfb914
GitHub-Pull-Request: #318
Reviewed-on: https://go-review.googlesource.com/c/tools/+/319689
Reviewed-by: Tim King <taking@google.com>
Trust: Tim King <taking@google.com>
Trust: Than McIntosh <thanm@google.com>
Run-TryBot: Tim King <taking@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
  • Loading branch information
KimMachineGun authored and timothy-king committed Oct 28, 2021
1 parent 9cd58b0 commit 8de2a7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions go/analysis/passes/sortslice/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
return
}

if fn.FullName() != "sort.Slice" {
fnName := fn.FullName()
if fnName != "sort.Slice" && fnName != "sort.SliceStable" && fnName != "sort.SliceIsSorted" {
return
}

Expand Down Expand Up @@ -115,7 +116,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
pass.Report(analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fmt.Sprintf("sort.Slice's argument must be a slice; is called with %s", typ.String()),
Message: fmt.Sprintf("%s's argument must be a slice; is called with %s", fnName, typ.String()),
SuggestedFixes: fixes,
})
})
Expand Down
10 changes: 10 additions & 0 deletions go/analysis/passes/sortslice/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ 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
Expand All @@ -23,6 +27,8 @@ func CorrectInterface() {
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 {
Expand All @@ -41,6 +47,8 @@ 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
Expand All @@ -51,4 +59,6 @@ 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)
}

0 comments on commit 8de2a7f

Please sign in to comment.