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

@@ -79,7 +79,7 @@ func GenerateGenesisStateFromDepositData(
// GenerateDepositsFromData a list of deposit items by creating proofs for each of them from a sparse Merkle trie.
func GenerateDepositsFromData(depositDataItems []*ethpb.Deposit_Data, trie *trie.SparseMerkleTrie) ([]*ethpb.Deposit, error) {
deposits := make([]*ethpb.Deposit, len(depositDataItems))
results, err := async.Scatter(len(depositDataItems), func(offset int, entries int, _ *sync.RWMutex) (interface{}, error) {
results, err := async.Scatter(len(depositDataItems), func(offset int, entries int, _ *sync.RWMutex) (any, error) {
return generateDepositsFromData(depositDataItems[offset:offset+entries], offset, trie)
})
if err != nil {
@@ -119,7 +119,7 @@ func DepositDataFromKeys(privKeys []bls.SecretKey, pubKeys []bls.PublicKey) ([]*
}
depositDataItems := make([]*ethpb.Deposit_Data, len(privKeys))
depositDataRoots := make([][]byte, len(privKeys))
results, err := async.Scatter(len(privKeys), func(offset int, entries int, _ *sync.RWMutex) (interface{}, error) {
results, err := async.Scatter(len(privKeys), func(offset int, entries int, _ *sync.RWMutex) (any, error) {
items, roots, err := depositDataFromKeys(privKeys[offset:offset+entries], pubKeys[offset:offset+entries], 0)
return &depositData{items: items, roots: roots}, err
})
@@ -145,7 +145,7 @@ func DepositDataFromKeysWithExecCreds(privKeys []bls.SecretKey, pubKeys []bls.Pu
func depositDataFromKeys(privKeys []bls.SecretKey, pubKeys []bls.PublicKey, numOfCreds uint64) ([]*ethpb.Deposit_Data, [][]byte, error) {
dataRoots := make([][]byte, len(privKeys))
depositDataItems := make([]*ethpb.Deposit_Data, len(privKeys))
for i := 0; i < len(privKeys); i++ {
for i := range privKeys {
withCred := uint64(i) < numOfCreds
data, err := createDepositData(privKeys[i], pubKeys[i], withCred)
if err != nil {

View File

@@ -27,7 +27,7 @@ func DeterministicallyGenerateKeys(startIndex, numKeys uint64) ([]bls.SecretKey,
publics []bls.PublicKey
}
// lint:ignore uintcast -- this is safe because we can reasonably expect that the number of keys is less than max int64.
results, err := async.Scatter(int(numKeys), func(offset int, entries int, _ *sync.RWMutex) (interface{}, error) {
results, err := async.Scatter(int(numKeys), func(offset int, entries int, _ *sync.RWMutex) (any, error) {
secs, pubs, err := deterministicallyGenerateKeys(uint64(offset)+startIndex, uint64(entries))
return &keys{secrets: secs, publics: pubs}, err
})

View File

@@ -372,7 +372,7 @@ func (s *PremineGenesisConfig) setInactivityScores(g state.BeaconState) error {
}
scoresMissing := len(g.Validators()) - len(scores)
if scoresMissing > 0 {
for i := 0; i < scoresMissing; i++ {
for range scoresMissing {
scores = append(scores, 0)
}
}
@@ -390,7 +390,7 @@ func (s *PremineGenesisConfig) setCurrentEpochParticipation(g state.BeaconState)
}
missing := len(g.Validators()) - len(p)
if missing > 0 {
for i := 0; i < missing; i++ {
for range missing {
p = append(p, 0)
}
}
@@ -408,7 +408,7 @@ func (s *PremineGenesisConfig) setPrevEpochParticipation(g state.BeaconState) er
}
missing := len(g.Validators()) - len(p)
if missing > 0 {
for i := 0; i < missing; i++ {
for range missing {
p = append(p, 0)
}
}
@@ -755,7 +755,7 @@ func unwrapUint64Ptr(u *uint64) uint64 {
func nZeroRoots(n uint64) [][]byte {
roots := make([][]byte, n)
zh := params.BeaconConfig().ZeroHash[:]
for i := uint64(0); i < n; i++ {
for i := range n {
roots[i] = zh
}
return roots
@@ -763,7 +763,7 @@ func nZeroRoots(n uint64) [][]byte {
func nSetRoots(n uint64, r []byte) [][]byte {
roots := make([][]byte, n)
for i := uint64(0); i < n; i++ {
for i := range n {
h := make([]byte, 32)
copy(h, r)
roots[i] = h

View File

@@ -343,7 +343,7 @@ func extractPrefix(msg string) (string, string) {
return prefix, msg
}
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}, appendSpace bool) error {
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value any, appendSpace bool) error {
b.WriteString(key)
b.WriteByte('=')
if err := f.appendValue(b, value); err != nil {
@@ -356,7 +356,7 @@ func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interf
return nil
}
func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) (err error) {
func (f *TextFormatter) appendValue(b *bytes.Buffer, value any) (err error) {
switch value := value.(type) {
case string:
if !f.needsQuoting(value) {

View File

@@ -83,7 +83,7 @@ func (s *ServiceRegistry) RegisterService(service Service) error {
// FetchService takes in a struct pointer and sets the value of that pointer
// to a service currently stored in the service registry. This ensures the input argument is
// set to the right pointer that refers to the originally registered service.
func (s *ServiceRegistry) FetchService(service interface{}) error {
func (s *ServiceRegistry) FetchService(service any) error {
if reflect.TypeOf(service).Kind() != reflect.Ptr {
return fmt.Errorf("input must be of pointer type, received value type instead: %T", service)
}

View File

@@ -63,11 +63,11 @@ func TestRegisterService_Different(t *testing.T) {
require.Equal(t, 2, len(registry.serviceTypes))
_, exists := registry.services[reflect.TypeOf(m)]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(m))
_, exists := registry.services[reflect.TypeFor[*mockService]()]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeFor[*mockService]())
_, exists = registry.services[reflect.TypeOf(s)]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeOf(s))
_, exists = registry.services[reflect.TypeFor[*secondMockService]()]
assert.Equal(t, true, exists, "service of type %v not registered", reflect.TypeFor[*secondMockService]())
}
func TestFetchService_OK(t *testing.T) {
@@ -104,6 +104,6 @@ func TestServiceStatus_OK(t *testing.T) {
statuses := registry.Statuses()
assert.ErrorContains(t, "something bad has happened", statuses[reflect.TypeOf(m)])
assert.ErrorContains(t, "woah, horsee", statuses[reflect.TypeOf(s)])
assert.ErrorContains(t, "something bad has happened", statuses[reflect.TypeFor[*mockService]()])
assert.ErrorContains(t, "woah, horsee", statuses[reflect.TypeFor[*secondMockService]()])
}