Add golang.org/x/tools modernize static analyzer and fix violations (#15946)

* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
This commit is contained in:
Preston Van Loon
2025-11-13 19:27:22 -06:00
committed by GitHub
parent f77b78943a
commit 2fd6bd8150
605 changed files with 217475 additions and 2228 deletions

View File

@@ -28,7 +28,7 @@ func (pq priorityQueue) Swap(i, j int) {
}
// Push a LeakyBucket to priorityQueue
func (pq *priorityQueue) Push(x interface{}) {
func (pq *priorityQueue) Push(x any) {
n := len(*pq)
b, ok := x.(*LeakyBucket)
if !ok {
@@ -38,7 +38,7 @@ func (pq *priorityQueue) Push(x interface{}) {
*pq = append(*pq, b)
}
func (pq *priorityQueue) Pop() interface{} {
func (pq *priorityQueue) Pop() any {
old := *pq
n := len(old)
b := old[n-1]

View File

@@ -34,7 +34,7 @@ func TestLen(t *testing.T) {
func TestPeak(t *testing.T) {
q := make(priorityQueue, 0, 4096)
for i := 0; i < 5; i++ {
for range 5 {
b := NewLeakyBucket(1.0, 5, time.Second)
q.Push(b)
}
@@ -43,7 +43,7 @@ func TestPeak(t *testing.T) {
func TestLess(t *testing.T) {
q := make(priorityQueue, 0, 4096)
for i := 0; i < 5; i++ {
for i := range 5 {
b := NewLeakyBucket(1.0, 5, time.Second)
b.p = now().Add(time.Duration(i))
q.Push(b)
@@ -59,7 +59,7 @@ func TestLess(t *testing.T) {
func TestSwap(t *testing.T) {
q := make(priorityQueue, 0, 4096)
for i := 0; i < 5; i++ {
for range 5 {
b := NewLeakyBucket(1.0, 5, time.Second)
q.Push(b)
}
@@ -80,7 +80,7 @@ func TestSwap(t *testing.T) {
func TestPush(t *testing.T) {
q := make(priorityQueue, 0, 4096)
for i := 0; i < 5; i++ {
for range 5 {
b := NewLeakyBucket(1.0, 5, time.Second)
q.Push(b)

View File

@@ -48,7 +48,7 @@ func TestNewLeakyBucket(t *testing.T) {
type actionSet struct {
count int64
action string
value interface{}
value any
}
type testSet struct {

View File

@@ -632,7 +632,7 @@ func BenchmarkValue(b *testing.B) {
b.Run("100,000 shared items", func(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _100k))
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(&testObject{})
}
})
@@ -641,11 +641,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _100k))
s.individualItems[0] = &MultiValueItem[int]{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}
objs := make([]*testObject, _100k)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[0].Values[0].ids = append(s.individualItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_100k)])
}
})
@@ -653,11 +653,11 @@ func BenchmarkValue(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _100k))
objs := make([]*testObject, _100k)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[uint64(i)] = &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}}
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_100k)])
}
})
@@ -666,11 +666,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _100k))
s.appendedItems = []*MultiValueItem[int]{{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}}
objs := make([]*testObject, _100k)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems[0].Values[0].ids = append(s.appendedItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_100k)])
}
})
@@ -679,18 +679,18 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _100k))
s.appendedItems = []*MultiValueItem[int]{}
objs := make([]*testObject, _100k)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems = append(s.appendedItems, &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}})
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_100k)])
}
})
b.Run("1,000,000 shared items", func(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _1m))
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(&testObject{})
}
})
@@ -699,11 +699,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _1m))
s.individualItems[0] = &MultiValueItem[int]{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}
objs := make([]*testObject, _1m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[0].Values[0].ids = append(s.individualItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_1m)])
}
})
@@ -711,11 +711,11 @@ func BenchmarkValue(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _1m))
objs := make([]*testObject, _1m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[uint64(i)] = &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}}
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_1m)])
}
})
@@ -724,11 +724,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _1m))
s.appendedItems = []*MultiValueItem[int]{{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}}
objs := make([]*testObject, _1m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems[0].Values[0].ids = append(s.appendedItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_1m)])
}
})
@@ -737,18 +737,18 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _1m))
s.appendedItems = []*MultiValueItem[int]{}
objs := make([]*testObject, _1m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems = append(s.appendedItems, &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}})
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_1m)])
}
})
b.Run("10,000,000 shared items", func(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _10m))
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(&testObject{})
}
})
@@ -757,11 +757,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _10m))
s.individualItems[0] = &MultiValueItem[int]{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}
objs := make([]*testObject, _10m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[0].Values[0].ids = append(s.individualItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_10m)])
}
})
@@ -769,11 +769,11 @@ func BenchmarkValue(b *testing.B) {
s := &Slice[int]{}
s.Init(make([]int, _10m))
objs := make([]*testObject, _10m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.individualItems[uint64(i)] = &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}}
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_10m)])
}
})
@@ -782,11 +782,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _10m))
s.appendedItems = []*MultiValueItem[int]{{Values: []*Value[int]{{val: 999, ids: []uint64{}}}}}
objs := make([]*testObject, _10m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems[0].Values[0].ids = append(s.appendedItems[0].Values[0].ids, uint64(i))
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_10m)])
}
})
@@ -795,11 +795,11 @@ func BenchmarkValue(b *testing.B) {
s.Init(make([]int, _10m))
s.appendedItems = []*MultiValueItem[int]{}
objs := make([]*testObject, _10m)
for i := 0; i < len(objs); i++ {
for i := range objs {
objs[i] = &testObject{id: uint64(i)}
s.appendedItems = append(s.appendedItems, &MultiValueItem[int]{Values: []*Value[int]{{val: i, ids: []uint64{uint64(i)}}}})
}
for i := 0; i < b.N; i++ {
for b.Loop() {
s.Value(objs[rand.Intn(_10m)])
}
})

View File

@@ -64,7 +64,7 @@ type Item struct {
Key string
// Value is an unspecified type that implementations can use to store
// information
Value interface{}
Value any
// Priority determines ordering in the queue, with the lowest value being the
// highest priority
@@ -185,7 +185,7 @@ func (q queue) Swap(i, j int) {
// Push is used by heap.Interface to push items onto the heap. This method is
// invoked by container/heap, and should not be used directly.
// See: https://golang.org/pkg/container/heap/#Interface
func (q *queue) Push(x interface{}) {
func (q *queue) Push(x any) {
n := len(*q)
item, ok := x.(*Item)
if !ok {
@@ -198,7 +198,7 @@ func (q *queue) Push(x interface{}) {
// Pop is used by heap.Interface to pop items off of the heap. This method is
// invoked by container/heap, and should not be used directly.
// See: https://golang.org/pkg/container/heap/#Interface
func (q *queue) Pop() interface{} {
func (q *queue) Pop() any {
old := *q
n := len(old)
item := old[n-1]

View File

@@ -2,7 +2,7 @@ package slice_test
import (
"reflect"
"sort"
"slices"
"testing"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
@@ -55,13 +55,11 @@ func TestIntersectionUint64(t *testing.T) {
{[]uint64{1, 1, 1}, []uint64{1, 1}, []uint64{1, 2, 3}, []uint64{1}},
}
for _, tt := range testCases {
setA := append([]uint64{}, tt.setA...)
setB := append([]uint64{}, tt.setB...)
setC := append([]uint64{}, tt.setC...)
setA := slices.Clone(tt.setA)
setB := slices.Clone(tt.setB)
setC := slices.Clone(tt.setC)
result := slice.IntersectionUint64(setA, setB, setC)
sort.Slice(result, func(i, j int) bool {
return result[i] < result[j]
})
slices.Sort(result)
if !reflect.DeepEqual(result, tt.out) {
t.Errorf("got %d, want %d", result, tt.out)
}
@@ -119,13 +117,11 @@ func TestIntersectionInt64(t *testing.T) {
{[]int64{1, 1, 1}, []int64{1, 1}, []int64{1, 2, 3}, []int64{1}},
}
for _, tt := range testCases {
setA := append([]int64{}, tt.setA...)
setB := append([]int64{}, tt.setB...)
setC := append([]int64{}, tt.setC...)
setA := slices.Clone(tt.setA)
setB := slices.Clone(tt.setB)
setC := slices.Clone(tt.setC)
result := slice.IntersectionInt64(setA, setB, setC)
sort.Slice(result, func(i, j int) bool {
return result[i] < result[j]
})
slices.Sort(result)
if !reflect.DeepEqual(result, tt.out) {
t.Errorf("got %d, want %d", result, tt.out)
}
@@ -525,13 +521,11 @@ func TestIntersectionSlot(t *testing.T) {
{[]primitives.Slot{1, 1, 1}, []primitives.Slot{1, 1}, []primitives.Slot{1, 2, 3}, []primitives.Slot{1}},
}
for _, tt := range testCases {
setA := append([]primitives.Slot{}, tt.setA...)
setB := append([]primitives.Slot{}, tt.setB...)
setC := append([]primitives.Slot{}, tt.setC...)
setA := slices.Clone(tt.setA)
setB := slices.Clone(tt.setB)
setC := slices.Clone(tt.setC)
result := slice.IntersectionSlot(setA, setB, setC)
sort.Slice(result, func(i, j int) bool {
return result[i] < result[j]
})
slices.Sort(result)
if !reflect.DeepEqual(result, tt.out) {
t.Errorf("got %d, want %d", result, tt.out)
}

View File

@@ -36,8 +36,8 @@ func BenchmarkMap_Concrete(b *testing.B) {
mm := &safeMap{
items: make(map[int]string),
}
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
for b.Loop() {
for j := range 1000 {
mm.Put(j, "foo")
mm.Get(j)
mm.Delete(j)
@@ -48,8 +48,8 @@ func BenchmarkMap_Concrete(b *testing.B) {
func BenchmarkMap_Generic(b *testing.B) {
items := make(map[int]string)
mm := NewThreadSafeMap(items)
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
for b.Loop() {
for j := range 1000 {
mm.Put(j, "foo")
mm.Get(j)
mm.Delete(j)
@@ -59,8 +59,8 @@ func BenchmarkMap_Generic(b *testing.B) {
func BenchmarkMap_GenericTx(b *testing.B) {
items := make(map[int]string)
mm := NewThreadSafeMap(items)
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
for b.Loop() {
for j := range 1000 {
mm.Do(func(mp map[int]string) {
mp[j] = "foo"
_ = mp[j]
@@ -85,7 +85,7 @@ func TestMap(t *testing.T) {
require.Equal(t, 3, tMap.Len())
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
for range 100 {
wg.Add(1)
go func(w *sync.WaitGroup, scopedMap *Map[int, string]) {
defer w.Done()

View File

@@ -82,7 +82,7 @@ func GenerateTrieFromItems(items [][]byte, depth uint64) (*SparseMerkleTrie, err
transformedLeaves[i] = arr[:]
}
layers[0] = transformedLeaves
for i := uint64(0); i < depth; i++ {
for i := range depth {
if len(layers[i])%2 == 1 {
layers[i] = append(layers[i], ZeroHashes[i][:])
}

View File

@@ -20,7 +20,7 @@ func TestCreateTrieFromProto_Validation(t *testing.T) {
h := hash.Hash([]byte("hi"))
genValidLayers := func(num int) []*ethpb.TrieLayer {
l := make([]*ethpb.TrieLayer, num)
for i := 0; i < num; i++ {
for i := range num {
l[i] = &ethpb.TrieLayer{
Layer: [][]byte{h[:]},
}
@@ -339,17 +339,17 @@ func BenchmarkGenerateTrieFromItems(b *testing.B) {
[]byte("FFFFFF"),
[]byte("GGGGGGG"),
}
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := trie.GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
require.NoError(b, err, "Could not generate Merkle trie from items")
}
}
func BenchmarkInsertTrie_Optimized(b *testing.B) {
b.StopTimer()
numDeposits := 16000
items := make([][]byte, numDeposits)
for i := 0; i < numDeposits; i++ {
for i := range numDeposits {
someRoot := bytesutil.ToBytes32([]byte(strconv.Itoa(i)))
items[i] = someRoot[:]
}
@@ -357,14 +357,14 @@ func BenchmarkInsertTrie_Optimized(b *testing.B) {
require.NoError(b, err)
someItem := bytesutil.ToBytes32([]byte("hello-world"))
b.StartTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
require.NoError(b, tr.Insert(someItem[:], i%numDeposits))
}
}
func BenchmarkGenerateProof(b *testing.B) {
b.StopTimer()
items := [][]byte{
[]byte("A"),
[]byte("BB"),
@@ -377,15 +377,14 @@ func BenchmarkGenerateProof(b *testing.B) {
normalTrie, err := trie.GenerateTrieFromItems(items, params.BeaconConfig().DepositContractTreeDepth)
require.NoError(b, err)
b.StartTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := normalTrie.MerkleProof(3)
require.NoError(b, err)
}
}
func BenchmarkVerifyMerkleProofWithDepth(b *testing.B) {
b.StopTimer()
items := [][]byte{
[]byte("A"),
[]byte("BB"),
@@ -402,8 +401,8 @@ func BenchmarkVerifyMerkleProofWithDepth(b *testing.B) {
root, err := m.HashTreeRoot()
require.NoError(b, err)
b.StartTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
if ok := trie.VerifyMerkleProofWithDepth(root[:], items[2], 2, proof, params.BeaconConfig().DepositContractTreeDepth); !ok {
b.Error("Merkle proof did not verify")
}