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

@@ -66,7 +66,7 @@ func (km *Keymanager) RecoverAccountsFromMnemonic(
}
privKeys := make([][]byte, numAccounts)
pubKeys := make([][]byte, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
privKey, err := util.PrivateKeyFromSeedAndPath(
seed, fmt.Sprintf(ValidatingKeyDerivationPathTemplate, i),
)
@@ -156,7 +156,7 @@ func (km *Keymanager) ListKeymanagerAccounts(ctx context.Context, cfg keymanager
} else {
fmt.Printf("Showing %d validator accounts\n", len(accountNames))
}
for i := 0; i < len(accountNames); i++ {
for i := range accountNames {
fmt.Println("")
validatingKeyPath := fmt.Sprintf(ValidatingKeyDerivationPathTemplate, i)

View File

@@ -99,7 +99,7 @@ func TestDerivedKeymanager_FetchValidatingPublicKeys(t *testing.T) {
require.Equal(t, numAccounts, len(publicKeys))
wantedPubKeys := make([][fieldparams.BLSPubkeyLength]byte, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
privKey, err := util.PrivateKeyFromSeedAndPath(derivedSeed, fmt.Sprintf(ValidatingKeyDerivationPathTemplate, i))
require.NoError(t, err)
var pubKey [fieldparams.BLSPubkeyLength]byte
@@ -138,7 +138,7 @@ func TestDerivedKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
require.Equal(t, numAccounts, len(privateKeys))
wantedPrivKeys := make([][32]byte, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
privKey, err := util.PrivateKeyFromSeedAndPath(derivedSeed, fmt.Sprintf(ValidatingKeyDerivationPathTemplate, i))
require.NoError(t, err)
var privKeyBytes [32]byte

View File

@@ -15,7 +15,7 @@ func TestLocalKeymanager_ExtractKeystores(t *testing.T) {
secretKeysCache = make(map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey)
dr := &Keymanager{}
validatingKeys := make([]bls.SecretKey, 10)
for i := 0; i < len(validatingKeys); i++ {
for i := range validatingKeys {
secretKey, err := bls.RandKey()
require.NoError(t, err)
validatingKeys[i] = secretKey

View File

@@ -29,7 +29,7 @@ func TestLocalKeymanager_DeleteKeystores(t *testing.T) {
ctx := t.Context()
keystores := make([]*keymanager.Keystore, numAccounts)
passwords := make([]string, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
keystores[i] = createRandomKeystore(t, password)
passwords[i] = password
}

View File

@@ -44,7 +44,7 @@ func (km *Keymanager) ImportKeystores(
for i := 0; i < len(storeCopy.PrivateKeys); i++ {
existingPubKeys[string(storeCopy.PublicKeys[i])] = true
}
for i := 0; i < len(keystores); i++ {
for i := range keystores {
var privKeyBytes []byte
var pubKeyBytes []byte
privKeyBytes, pubKeyBytes, _, err = km.attemptDecryptKeystore(decryptor, keystores[i], passwords[i])

View File

@@ -41,7 +41,7 @@ func TestLocalKeymanager_NoDuplicates(t *testing.T) {
numKeys := 50
pubKeys := make([][]byte, numKeys)
privKeys := make([][]byte, numKeys)
for i := 0; i < numKeys; i++ {
for i := range numKeys {
priv, err := bls.RandKey()
require.NoError(t, err)
privKeys[i] = priv.Marshal()
@@ -111,7 +111,7 @@ func TestLocalKeymanager_ImportKeystores(t *testing.T) {
numKeystores := 5
keystores := make([]*keymanager.Keystore, numKeystores)
passwords := make([]string, numKeystores)
for i := 0; i < numKeystores; i++ {
for i := range numKeystores {
keystores[i] = createRandomKeystore(t, password)
passwords[i] = password
}
@@ -131,7 +131,7 @@ func TestLocalKeymanager_ImportKeystores(t *testing.T) {
numKeystores := 5
keystores := make([]*keymanager.Keystore, numKeystores)
passwords := make([]string, numKeystores)
for i := 0; i < numKeystores; i++ {
for i := range numKeystores {
pass := password + strconv.Itoa(i)
keystores[i] = createRandomKeystore(t, pass)
passwords[i] = pass

View File

@@ -71,10 +71,10 @@ func (a *accountStore) Copy() *accountStore {
// AccountsKeystoreRepresentation defines an internal Prysm representation
// of validator accounts, encrypted according to the EIP-2334 standard.
type AccountsKeystoreRepresentation struct {
Crypto map[string]interface{} `json:"crypto"`
ID string `json:"uuid"`
Version uint `json:"version"`
Name string `json:"name"`
Crypto map[string]any `json:"crypto"`
ID string `json:"uuid"`
Version uint `json:"version"`
Name string `json:"name"`
}
// ResetCaches for the keymanager.
@@ -127,7 +127,7 @@ func NewInteropKeymanager(_ context.Context, offset, numValidatorKeys uint64) (*
}
lock.Lock()
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, numValidatorKeys)
for i := uint64(0); i < numValidatorKeys; i++ {
for i := range numValidatorKeys {
publicKey := bytesutil.ToBytes48(publicKeys[i].Marshal())
pubKeys[i] = publicKey
secretKeysCache[publicKey] = secretKeys[i]
@@ -374,7 +374,7 @@ func updateAccountsStoreKeys(store *accountStore, privateKeys, publicKeys [][]by
}
// We append to the accounts store keys only
// if the private/secret key do not already exist, to prevent duplicates.
for i := 0; i < len(privateKeys); i++ {
for i := range privateKeys {
sk := privateKeys[i]
pk := publicKeys[i]
_, privKeyExists := existingPrivKeys[string(sk)]
@@ -414,7 +414,7 @@ func (km *Keymanager) ListKeymanagerAccounts(ctx context.Context, cfg keymanager
return errors.Wrap(err, "could not fetch private keys")
}
}
for i := 0; i < len(accountNames); i++ {
for i := range accountNames {
fmt.Println("")
fmt.Printf("%s | %s\n", au.BrightBlue(fmt.Sprintf("Account %d", i)).Bold(), au.BrightGreen(accountNames[i]).Bold())
fmt.Printf("%s %#x\n", au.BrightMagenta("[validating public key]").Bold(), pubKeys[i])
@@ -429,12 +429,12 @@ func (km *Keymanager) ListKeymanagerAccounts(ctx context.Context, cfg keymanager
}
func CreatePrintoutOfKeys(keys [][]byte) string {
var keysStr string
var keysStr strings.Builder
for i, k := range keys {
if i != 0 {
keysStr += "," // Add a comma before each key except the first one
keysStr.WriteString(",") // Add a comma before each key except the first one
}
keysStr += fmt.Sprintf("%#x", bytesutil.Trunc(k))
keysStr.WriteString(fmt.Sprintf("%#x", bytesutil.Trunc(k)))
}
return keysStr
return keysStr.String()
}

View File

@@ -29,7 +29,7 @@ func TestLocalKeymanager_FetchValidatingPublicKeys(t *testing.T) {
ctx := t.Context()
numAccounts := 10
wantedPubKeys := make([][fieldparams.BLSPubkeyLength]byte, 0)
for i := 0; i < numAccounts; i++ {
for range numAccounts {
privKey, err := bls.RandKey()
require.NoError(t, err)
pubKey := bytesutil.ToBytes48(privKey.PublicKey().Marshal())
@@ -61,7 +61,7 @@ func TestLocalKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
ctx := t.Context()
numAccounts := 10
wantedPrivateKeys := make([][32]byte, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
privKey, err := bls.RandKey()
require.NoError(t, err)
privKeyData := privKey.Marshal()
@@ -97,7 +97,7 @@ func TestLocalKeymanager_Sign(t *testing.T) {
numAccounts := 10
keystores := make([]*keymanager.Keystore, numAccounts)
passwords := make([]string, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
keystores[i] = createRandomKeystore(t, password)
passwords[i] = password
}

View File

@@ -53,13 +53,13 @@ func (km *Keymanager) listenForAccountChanges(ctx context.Context) {
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
fileChangesChan := make(chan interface{}, 100)
fileChangesChan := make(chan any, 100)
defer close(fileChangesChan)
// We debounce events sent over the file changes channel by an interval
// to ensure we are not overwhelmed by a ton of events fired over the channel in
// a short span of time.
go async.Debounce(ctx, debounceFileChangesInterval, fileChangesChan, func(event interface{}) {
go async.Debounce(ctx, debounceFileChangesInterval, fileChangesChan, func(event any) {
ev, ok := event.(fsnotify.Event)
if !ok {
log.Errorf("Type %T is not a valid file system event", event)

View File

@@ -60,7 +60,7 @@ func TestLocalKeymanager_reloadAccountsFromKeystore(t *testing.T) {
numAccounts := 20
privKeys := make([][]byte, numAccounts)
pubKeys := make([][]byte, numAccounts)
for i := 0; i < numAccounts; i++ {
for i := range numAccounts {
privKey, err := bls.RandKey()
require.NoError(t, err)
privKeys[i] = privKey.Marshal()

View File

@@ -189,7 +189,7 @@ func (client *ApiClient) doRequest(ctx context.Context, httpMethod, fullPath str
}
// unmarshalResponse is a utility method for unmarshalling responses.
func unmarshalResponse(responseBody io.ReadCloser, unmarshalledResponseObject interface{}) error {
func unmarshalResponse(responseBody io.ReadCloser, unmarshalledResponseObject any) error {
defer closeBody(responseBody)
if err := json.NewDecoder(responseBody).Decode(&unmarshalledResponseObject); err != nil {
body, err := io.ReadAll(responseBody)

View File

@@ -756,7 +756,7 @@ func (km *Keymanager) ListKeymanagerAccounts(ctx context.Context, cfg keymanager
// DisplayRemotePublicKeys prints remote public keys to stdout.
func DisplayRemotePublicKeys(validatingPubKeys [][48]byte) {
au := aurora.NewAurora(true)
for i := 0; i < len(validatingPubKeys); i++ {
for i := range validatingPubKeys {
fmt.Println("")
fmt.Printf(
"%s\n", au.BrightGreen(petnames.DeterministicName(validatingPubKeys[i][:], "-")).Bold(),

View File

@@ -100,13 +100,13 @@ type AccountLister interface {
// Keystore json file representation as a Go struct.
type Keystore struct {
Crypto map[string]interface{} `json:"crypto"`
ID string `json:"uuid"`
Pubkey string `json:"pubkey"`
Version uint `json:"version"`
Description string `json:"description"`
Name string `json:"name,omitempty"` // field deprecated in favor of description, EIP2335
Path string `json:"path"`
Crypto map[string]any `json:"crypto"`
ID string `json:"uuid"`
Pubkey string `json:"pubkey"`
Version uint `json:"version"`
Description string `json:"description"`
Name string `json:"name,omitempty"` // field deprecated in favor of description, EIP2335
Path string `json:"path"`
}
// Kind defines an enum for either local, derived, or remote-signing