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

@@ -17,8 +17,7 @@ func BenchmarkSignature_Verify(b *testing.B) {
msg := []byte("Some msg")
sig := sk.Sign(msg)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
if !sig.Verify(sk.PublicKey(), msg) {
b.Fatal("could not verify sig")
}
@@ -31,7 +30,7 @@ func BenchmarkSignature_AggregateVerify(b *testing.B) {
var pks []common.PublicKey
var sigs []common.Signature
var msgs [][32]byte
for i := 0; i < sigN; i++ {
for i := range sigN {
msg := [32]byte{'s', 'i', 'g', 'n', 'e', 'd', byte(i)}
sk, err := blst.RandKey()
require.NoError(b, err)
@@ -42,9 +41,8 @@ func BenchmarkSignature_AggregateVerify(b *testing.B) {
}
aggregated := blst.AggregateSignatures(sigs)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
if !aggregated.AggregateVerify(pks, msgs) {
b.Fatal("could not verify aggregate sig")
}
@@ -56,8 +54,7 @@ func BenchmarkSecretKey_Marshal(b *testing.B) {
require.NoError(b, err)
d := key.Marshal()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := blst.SecretKeyFromBytes(d)
_ = err
}

View File

@@ -95,7 +95,7 @@ func TestPublicKey_Aggregate(t *testing.T) {
func TestPublicKey_Aggregation_NoCorruption(t *testing.T) {
var pubkeys []common.PublicKey
for i := 0; i < 100; i++ {
for range 100 {
priv, err := blst.RandKey()
require.NoError(t, err)
pubkey := priv.PublicKey()
@@ -113,54 +113,40 @@ func TestPublicKey_Aggregation_NoCorruption(t *testing.T) {
wg := new(sync.WaitGroup)
// Aggregate different sets of keys.
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys)
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[:10])
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[:40])
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[20:60])
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[80:])
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[60:90])
require.NoError(t, err)
wg.Done()
}()
})
wg.Add(1)
go func() {
wg.Go(func() {
_, err := blst.AggregatePublicKeys(compressedKeys[40:99])
require.NoError(t, err)
wg.Done()
}()
})
wg.Wait()
@@ -185,7 +171,7 @@ func BenchmarkPublicKeyFromBytes(b *testing.B) {
b.Run("cache on", func(b *testing.B) {
blst.EnableCaches()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := blst.PublicKeyFromBytes(pubkeyBytes)
require.NoError(b, err)
}
@@ -193,7 +179,7 @@ func BenchmarkPublicKeyFromBytes(b *testing.B) {
b.Run("cache off", func(b *testing.B) {
blst.DisableCaches()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := blst.PublicKeyFromBytes(pubkeyBytes)
require.NoError(b, err)
}

View File

@@ -144,7 +144,7 @@ func (s *Signature) AggregateVerify(pubKeys []common.PublicKey, msgs [][32]byte)
}
msgSlices := make([][]byte, len(msgs))
rawKeys := make([]*blstPublicKey, len(msgs))
for i := 0; i < size; i++ {
for i := range size {
msgSlices[i] = msgs[i][:]
rawKeys[i] = pubKeys[i].(*PublicKey).p
}
@@ -168,7 +168,7 @@ func (s *Signature) FastAggregateVerify(pubKeys []common.PublicKey, msg [32]byte
return false
}
rawKeys := make([]*blstPublicKey, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
rawKeys[i] = pubKeys[i].(*PublicKey).p
}
return s.s.FastAggregateVerify(true, rawKeys, msg[:], dst)
@@ -206,7 +206,7 @@ func AggregateSignatures(sigs []common.Signature) common.Signature {
}
rawSigs := make([]*blstSignature, len(sigs))
for i := 0; i < len(sigs); i++ {
for i := range sigs {
rawSigs[i] = sigs[i].(*Signature).s
}
@@ -246,7 +246,7 @@ func VerifyMultipleSignatures(sigs [][]byte, msgs [][32]byte, pubKeys []common.P
mulP1Aff := make([]*blstPublicKey, length)
rawMsgs := make([]blst.Message, length)
for i := 0; i < length; i++ {
for i := range length {
mulP1Aff[i] = pubKeys[i].(*PublicKey).p
rawMsgs[i] = msgs[i][:]
}

View File

@@ -25,7 +25,7 @@ func TestAggregateVerify(t *testing.T) {
pubkeys := make([]common.PublicKey, 0, 100)
sigs := make([]common.Signature, 0, 100)
var msgs [][32]byte
for i := 0; i < 100; i++ {
for i := range 100 {
msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
priv, err := RandKey()
require.NoError(t, err)
@@ -45,7 +45,7 @@ func TestAggregateVerify_CompressedSignatures(t *testing.T) {
sigs := make([]common.Signature, 0, 100)
var sigBytes [][]byte
var msgs [][32]byte
for i := 0; i < 100; i++ {
for i := range 100 {
msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
priv, err := RandKey()
require.NoError(t, err)
@@ -69,7 +69,7 @@ func TestFastAggregateVerify(t *testing.T) {
pubkeys := make([]common.PublicKey, 0, 100)
sigs := make([]common.Signature, 0, 100)
msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
for i := 0; i < 100; i++ {
for range 100 {
priv, err := RandKey()
require.NoError(t, err)
pub := priv.PublicKey()
@@ -119,7 +119,7 @@ func TestMultipleSignatureVerification(t *testing.T) {
pubkeys := make([]common.PublicKey, 0, 100)
sigs := make([][]byte, 0, 100)
var msgs [][32]byte
for i := 0; i < 100; i++ {
for i := range 100 {
msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
priv, err := RandKey()
require.NoError(t, err)
@@ -146,7 +146,7 @@ func TestEth2FastAggregateVerify(t *testing.T) {
pubkeys := make([]common.PublicKey, 0, 100)
sigs := make([]common.Signature, 0, 100)
msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
for i := 0; i < 100; i++ {
for range 100 {
priv, err := RandKey()
require.NoError(t, err)
pub := priv.PublicKey()

View File

@@ -3,6 +3,7 @@ package bls
import (
"encoding/hex"
"fmt"
"strings"
"github.com/pkg/errors"
)
@@ -54,7 +55,8 @@ func (s *SignatureBatch) VerifyVerbosely() (bool, error) {
// if signature batch is invalid, we then verify signatures one by one.
errmsg := "some signatures are invalid. details:"
var errmsg strings.Builder
errmsg.WriteString("some signatures are invalid. details:")
for i := 0; i < len(s.Signatures); i++ {
sig := s.Signatures[i]
msg := s.Messages[i]
@@ -64,20 +66,20 @@ func (s *SignatureBatch) VerifyVerbosely() (bool, error) {
if !valid {
desc := s.Descriptions[i]
if err != nil {
errmsg += fmt.Sprintf("\nsignature '%s' is invalid."+
errmsg.WriteString(fmt.Sprintf("\nsignature '%s' is invalid."+
" signature: 0x%s, public key: 0x%s, message: 0x%v, error: %v",
desc, hex.EncodeToString(sig), hex.EncodeToString(pubKey.Marshal()),
hex.EncodeToString(msg[:]), err)
hex.EncodeToString(msg[:]), err))
} else {
errmsg += fmt.Sprintf("\nsignature '%s' is invalid."+
errmsg.WriteString(fmt.Sprintf("\nsignature '%s' is invalid."+
" signature: 0x%s, public key: 0x%s, message: 0x%v",
desc, hex.EncodeToString(sig), hex.EncodeToString(pubKey.Marshal()),
hex.EncodeToString(msg[:]))
hex.EncodeToString(msg[:])))
}
}
}
return false, errors.New(errmsg)
return false, errors.New(errmsg.String())
}
// Copy the attached signature batch and return it

View File

@@ -90,7 +90,7 @@ func TestVerifyVerbosely_VerificationThrowsError(t *testing.T) {
func TestSignatureBatch_RemoveDuplicates(t *testing.T) {
var keys []SecretKey
for i := 0; i < 100; i++ {
for range 100 {
key, err := RandKey()
assert.NoError(t, err)
keys = append(keys, key)
@@ -379,7 +379,7 @@ func TestSignatureBatch_RemoveDuplicates(t *testing.T) {
func TestSignatureBatch_AggregateBatch(t *testing.T) {
var keys []SecretKey
for i := 0; i < 100; i++ {
for range 100 {
key, err := RandKey()
assert.NoError(t, err)
keys = append(keys, key)
@@ -654,7 +654,7 @@ func NewValidSignatureSet(t *testing.T, msgBody string, num int) *SignatureBatch
Descriptions: make([]string, num),
}
for i := 0; i < num; i++ {
for i := range num {
priv, err := RandKey()
require.NoError(t, err)
pubkey := priv.PublicKey()
@@ -679,7 +679,7 @@ func NewInvalidSignatureSet(t *testing.T, msgBody string, num int, throwErr bool
Descriptions: make([]string, num),
}
for i := 0; i < num; i++ {
for i := range num {
priv, err := RandKey()
require.NoError(t, err)
pubkey := priv.PublicKey()

View File

@@ -19,7 +19,7 @@ import (
// or has nil objects within lists.
var ErrNilProto = errors.New("cannot hash a nil protobuf message")
var sha256Pool = sync.Pool{New: func() interface{} {
var sha256Pool = sync.Pool{New: func() any {
return sha256.New()
}}
@@ -75,7 +75,7 @@ func CustomSHA256Hasher() func([]byte) [32]byte {
}
}
var keccak256Pool = sync.Pool{New: func() interface{} {
var keccak256Pool = sync.Pool{New: func() any {
return sha3.NewLegacyKeccak256()
}}

View File

@@ -26,7 +26,7 @@ func TestHash(t *testing.T) {
}
func BenchmarkHash(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
hash.Hash([]byte("abc"))
}
}
@@ -50,7 +50,7 @@ func TestHashKeccak256(t *testing.T) {
}
func BenchmarkHashKeccak256(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
hash.Keccak256([]byte("abc"))
}
}
@@ -77,7 +77,7 @@ func TestHashProtoFuzz(t *testing.T) {
}
}(t)
for i := 0; i < 1000; i++ {
for range 1000 {
msg := &pb.AddressBook{}
f.Fuzz(msg)
_, err := hash.Proto(msg)
@@ -98,7 +98,7 @@ func BenchmarkHashProto(b *testing.B) {
Signature: bls.NewAggregateSignature().Marshal(),
}
for i := 0; i < b.N; i++ {
for b.Loop() {
if _, err := hash.Proto(att); err != nil {
b.Log(err)
}

View File

@@ -35,7 +35,7 @@ func VectorizedSha256(inputList [][32]byte) [][32]byte {
wg := sync.WaitGroup{}
wg.Add(n)
groupSize := len(inputList) / (2 * (n + 1))
for j := 0; j < n; j++ {
for j := range n {
go hashParallel(inputList[j*2*groupSize:(j+1)*2*groupSize], outputList[j*groupSize:], &wg)
}
err := gohashtree.Hash(outputList[n*groupSize:], inputList[n*2*groupSize:])

View File

@@ -12,12 +12,10 @@ func Test_VectorizedSha256(t *testing.T) {
secondLargeSlice := make([][32]byte, 32*minSliceSizeToParallelize)
hash1 := make([][32]byte, 16*minSliceSizeToParallelize)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
tempHash := VectorizedSha256(largeSlice)
copy(hash1, tempHash)
}()
})
wg.Wait()
hash2 := VectorizedSha256(secondLargeSlice)
require.Equal(t, len(hash1), len(hash2))

View File

@@ -75,12 +75,12 @@ type encryptedKeyJSON struct {
}
type cryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]any `json:"kdfparams"`
MAC string `json:"mac"`
}
type cipherparamsJSON struct {

View File

@@ -153,7 +153,7 @@ func EncryptKey(key *Key, password string, scryptN, scryptP int) ([]byte, error)
mac := Keccak256(derivedKey[16:32], cipherText)
scryptParamsJSON := make(map[string]interface{}, 5)
scryptParamsJSON := make(map[string]any, 5)
scryptParamsJSON["n"] = scryptN
scryptParamsJSON["r"] = scryptR
scryptParamsJSON["p"] = scryptP

View File

@@ -41,7 +41,7 @@ func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
return outText, err
}
func ensureInt(x interface{}) int {
func ensureInt(x any) int {
res, ok := x.(int)
if !ok {
res = int(x.(float64))

View File

@@ -48,7 +48,7 @@ func TestGetRandBlobElements(t *testing.T) {
blob := GetRandBlob(seed)
// Check that each field element in the blob matches what we'd get from GetRandFieldElement
for i := 0; i < GoKZG.ScalarsPerBlob; i++ {
for i := range GoKZG.ScalarsPerBlob {
start := i * GoKZG.SerializedScalarSize
end := start + GoKZG.SerializedScalarSize