From e9d3b030ed6fe8380d9b0411ef06eff001769641 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 2 Jun 2025 11:27:08 -0400 Subject: [PATCH] slices,sort: explicitly discard results in benchmarks The unusedresult analyzer will report failure to use the results of these pure functions. Updates #73950 Change-Id: I783cb92ad913105afd46c782bedf6234410c645d Reviewed-on: https://go-review.googlesource.com/c/go/+/677995 Reviewed-by: Dmitri Shuralyov Commit-Queue: Alan Donovan Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- src/slices/sort_benchmark_test.go | 4 ++-- src/slices/sort_test.go | 8 ++++---- src/sort/sort_slices_benchmark_test.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/slices/sort_benchmark_test.go b/src/slices/sort_benchmark_test.go index 1dde26ef1c..cafb1a4618 100644 --- a/src/slices/sort_benchmark_test.go +++ b/src/slices/sort_benchmark_test.go @@ -23,7 +23,7 @@ func BenchmarkBinarySearchFloats(b *testing.B) { needle := (floats[midpoint] + floats[midpoint+1]) / 2 b.ResetTimer() for i := 0; i < b.N; i++ { - slices.BinarySearch(floats, needle) + _, _ = slices.BinarySearch(floats, needle) } }) } @@ -46,7 +46,7 @@ func BenchmarkBinarySearchFuncStruct(b *testing.B) { cmpFunc := func(a, b *myStruct) int { return a.n - b.n } b.ResetTimer() for i := 0; i < b.N; i++ { - slices.BinarySearchFunc(structs, needle, cmpFunc) + _, _ = slices.BinarySearchFunc(structs, needle, cmpFunc) } }) } diff --git a/src/slices/sort_test.go b/src/slices/sort_test.go index 2e045e2af8..855f861c51 100644 --- a/src/slices/sort_test.go +++ b/src/slices/sort_test.go @@ -264,19 +264,19 @@ func TestMinMaxPanics(t *testing.T) { intCmp := func(a, b int) int { return a - b } emptySlice := []int{} - if !panics(func() { Min(emptySlice) }) { + if !panics(func() { _ = Min(emptySlice) }) { t.Errorf("Min([]): got no panic, want panic") } - if !panics(func() { Max(emptySlice) }) { + if !panics(func() { _ = Max(emptySlice) }) { t.Errorf("Max([]): got no panic, want panic") } - if !panics(func() { MinFunc(emptySlice, intCmp) }) { + if !panics(func() { _ = MinFunc(emptySlice, intCmp) }) { t.Errorf("MinFunc([]): got no panic, want panic") } - if !panics(func() { MaxFunc(emptySlice, intCmp) }) { + if !panics(func() { _ = MaxFunc(emptySlice, intCmp) }) { t.Errorf("MaxFunc([]): got no panic, want panic") } } diff --git a/src/sort/sort_slices_benchmark_test.go b/src/sort/sort_slices_benchmark_test.go index 069536df03..6fea511284 100644 --- a/src/sort/sort_slices_benchmark_test.go +++ b/src/sort/sort_slices_benchmark_test.go @@ -85,7 +85,7 @@ func BenchmarkSlicesIsSorted(b *testing.B) { b.StopTimer() ints := makeSortedInts(N) b.StartTimer() - slices.IsSorted(ints) + _ = slices.IsSorted(ints) } }