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

@@ -42,7 +42,7 @@ func TestServer_AuthenticateUsingExistingToken(t *testing.T) {
unaryInfo := &grpc.UnaryServerInfo{
FullMethod: "Proto.CreateWallet",
}
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
unaryHandler := func(ctx context.Context, req any) (any, error) {
return nil, nil
}
ctxMD := map[string][]string{

View File

@@ -74,7 +74,7 @@ func TestServer_CreateWallet_Local(t *testing.T) {
encryptor := keystorev4.New()
keystores := make([]string, 3)
passwords := make([]string, 3)
for i := 0; i < len(keystores); i++ {
for i := range keystores {
privKey, err := bls.RandKey()
require.NoError(t, err)
pubKey := fmt.Sprintf("%x", privKey.PublicKey().Marshal())

View File

@@ -76,7 +76,7 @@ func (s *Server) ListAccounts(w http.ResponseWriter, r *http.Request) {
return
}
accs := make([]*Account, len(keys))
for i := 0; i < len(keys); i++ {
for i := range keys {
accs[i] = &Account{
ValidatingPublicKey: hexutil.Encode(keys[i][:]),
AccountName: petnames.DeterministicName(keys[i][:], "-"),

View File

@@ -38,7 +38,7 @@ func (m *MockBeaconNodeHealthClient) Recv() (*pb.LogsResponse, error) {
return log, nil
}
func (m *MockBeaconNodeHealthClient) SendMsg(_ interface{}) error {
func (m *MockBeaconNodeHealthClient) SendMsg(_ any) error {
return m.err
}

View File

@@ -63,7 +63,7 @@ func (s *Server) ListKeystores(w http.ResponseWriter, r *http.Request) {
return
}
keystoreResponse := make([]*Keystore, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
keystoreResponse[i] = &Keystore{
ValidatingPubkey: hexutil.Encode(pubKeys[i][:]),
}
@@ -276,7 +276,7 @@ func (s *Server) transformDeletedKeysStatuses(
return nil, errors.Wrap(err, "could not get public keys from DB")
}
if len(pubKeysInDB) > 0 {
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
keyExistsInDB := pubKeysInDB[bytesutil.ToBytes48(pubKeys[i])]
if keyExistsInDB && statuses[i].Status == keymanager.StatusNotFound {
statuses[i].Status = keymanager.StatusNotActive
@@ -419,7 +419,7 @@ func (s *Server) ListRemoteKeys(w http.ResponseWriter, r *http.Request) {
return
}
keystoreResponse := make([]*RemoteKey, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
keystoreResponse[i] = &RemoteKey{
Pubkey: hexutil.Encode(pubKeys[i][:]),
Url: s.validatorService.RemoteSignerConfig().BaseEndpoint,

View File

@@ -109,7 +109,7 @@ func TestServer_ListKeystores(t *testing.T) {
resp := &ListKeystoresResponse{}
require.NoError(t, json.Unmarshal(wr.Body.Bytes(), resp))
require.Equal(t, numAccounts, len(resp.Data))
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
require.DeepEqual(t, hexutil.Encode(expectedKeys[i][:]), resp.Data[i].ValidatingPubkey)
require.Equal(
t,
@@ -243,7 +243,7 @@ func TestServer_ImportKeystores(t *testing.T) {
password := "12345678"
encodedKeystores := make([]string, numKeystores)
passwords := make([]string, numKeystores)
for i := 0; i < numKeystores; i++ {
for i := range numKeystores {
enc, err := json.Marshal(createRandomKeystore(t, password))
encodedKeystores[i] = string(enc)
require.NoError(t, err)
@@ -280,7 +280,7 @@ func TestServer_ImportKeystores(t *testing.T) {
keystores := make([]*keymanager.Keystore, numKeystores)
passwords := make([]string, numKeystores)
publicKeys := make([][fieldparams.BLSPubkeyLength]byte, numKeystores)
for i := 0; i < numKeystores; i++ {
for i := range numKeystores {
keystores[i] = createRandomKeystore(t, password)
pubKey, err := hexutil.Decode("0x" + keystores[i].Pubkey)
require.NoError(t, err)
@@ -307,7 +307,7 @@ func TestServer_ImportKeystores(t *testing.T) {
require.NoError(t, validatorDB.Close())
}()
encodedKeystores := make([]string, numKeystores)
for i := 0; i < numKeystores; i++ {
for i := range numKeystores {
enc, err := json.Marshal(keystores[i])
require.NoError(t, err)
encodedKeystores[i] = string(enc)
@@ -316,7 +316,7 @@ func TestServer_ImportKeystores(t *testing.T) {
// Generate mock slashing history.
attestingHistory := make([][]*dbCommon.AttestationRecord, 0)
proposalHistory := make([]dbCommon.ProposalHistoryForPubkey, len(publicKeys))
for i := 0; i < len(publicKeys); i++ {
for i := range publicKeys {
proposalHistory[i].Proposals = make([]dbCommon.Proposal, 0)
}
mockJSON, err := mocks.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)
@@ -439,7 +439,7 @@ func TestServer_DeleteKeystores(t *testing.T) {
// Generate mock slashing history.
attestingHistory := make([][]*dbCommon.AttestationRecord, 0)
proposalHistory := make([]dbCommon.ProposalHistoryForPubkey, len(publicKeys))
for i := 0; i < len(publicKeys); i++ {
for i := range publicKeys {
proposalHistory[i].Proposals = make([]dbCommon.Proposal, 0)
}
mockJSON, err := mocks.MockSlashingProtectionJSON(publicKeys, attestingHistory, proposalHistory)

View File

@@ -93,7 +93,7 @@ func TestImportSlashingProtection_Preconditions(t *testing.T) {
// Generate mock slashing history.
attestingHistory := make([][]*common.AttestationRecord, 0)
proposalHistory := make([]common.ProposalHistoryForPubkey, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
proposalHistory[i].Proposals = make([]common.Proposal, 0)
}
mockJSON, err := mocks.MockSlashingProtectionJSON(pubKeys, attestingHistory, proposalHistory)
@@ -198,7 +198,7 @@ func TestImportExportSlashingProtection_RoundTrip(t *testing.T) {
// Generate mock slashing history.
attestingHistory := make([][]*common.AttestationRecord, 0)
proposalHistory := make([]common.ProposalHistoryForPubkey, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
for i := range pubKeys {
proposalHistory[i].Proposals = make([]common.Proposal, 0)
}
mockJSON, err := mocks.MockSlashingProtectionJSON(pubKeys, attestingHistory, proposalHistory)

View File

@@ -18,10 +18,10 @@ import (
func (s *Server) AuthTokenInterceptor() grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req interface{},
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
) (any, error) {
if err := s.authorize(ctx); err != nil {
return nil, err
}

View File

@@ -24,7 +24,7 @@ func TestServer_AuthTokenInterceptor_Verify(t *testing.T) {
unaryInfo := &grpc.UnaryServerInfo{
FullMethod: "Proto.CreateWallet",
}
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
unaryHandler := func(ctx context.Context, req any) (any, error) {
return nil, nil
}
ctxMD := map[string][]string{
@@ -45,7 +45,7 @@ func TestServer_AuthTokenInterceptor_BadToken(t *testing.T) {
unaryInfo := &grpc.UnaryServerInfo{
FullMethod: "Proto.CreateWallet",
}
unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
unaryHandler := func(ctx context.Context, req any) (any, error) {
return nil, nil
}