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

@@ -444,14 +444,14 @@ func FuzzKmpIndex(f *testing.F) {
// Parse comma-separated strings into int slices
var source, target []int
if sourceStr != "" {
for _, s := range strings.Split(sourceStr, ",") {
for s := range strings.SplitSeq(sourceStr, ",") {
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
source = append(source, val)
}
}
}
if targetStr != "" {
for _, s := range strings.Split(targetStr, ",") {
for s := range strings.SplitSeq(targetStr, ",") {
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
target = append(target, val)
}
@@ -508,7 +508,7 @@ func FuzzComputeLPS(f *testing.F) {
// Parse comma-separated string into int slice
var pattern []int
if patternStr != "" {
for _, s := range strings.Split(patternStr, ",") {
for s := range strings.SplitSeq(patternStr, ",") {
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
pattern = append(pattern, val)
}

View File

@@ -47,10 +47,11 @@ func FuzzPropertyRoundTrip(f *testing.F) {
ctx := t.Context()
// Create source state with reasonable size
validatorCount := uint64(len(validatorChanges) + 8) // Minimum 8 validators
if validatorCount > 64 {
validatorCount = 64 // Cap at 64 for performance
}
validatorCount := min(
// Minimum 8 validators
uint64(len(validatorChanges)+8),
// Cap at 64 for performance
64)
source, _ := util.DeterministicGenesisStateElectra(t, validatorCount)
// Create target state with modifications

View File

@@ -63,7 +63,7 @@ func TestReasonablePerformance(t *testing.T) {
// Make realistic changes
_ = target.SetSlot(source.Slot() + 32) // One epoch
validators := target.Validators()
for i := 0; i < 100; i++ { // 10% of validators changed
for i := range 100 { // 10% of validators changed
validators[i].EffectiveBalance += 1000000000 // 1 ETH change
}
_ = target.SetValidators(validators)
@@ -129,7 +129,7 @@ func TestStateTransitionValidation(t *testing.T) {
// Some validators get rewards, others get penalties
balances := target.Balances()
for i := 0; i < len(balances); i++ {
for i := range balances {
if i%2 == 0 {
balances[i] += 100000000 // 0.1 ETH reward
} else {
@@ -331,12 +331,12 @@ func TestConcurrencySafety(t *testing.T) {
var wg sync.WaitGroup
errors := make(chan error, numGoroutines*iterations)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
for j := range iterations {
_, err := Diff(source, target)
if err != nil {
errors <- fmt.Errorf("worker %d iteration %d: %v", workerID, j, err)
@@ -367,7 +367,7 @@ func TestConcurrencySafety(t *testing.T) {
var wg sync.WaitGroup
errors := make(chan error, numGoroutines)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
wg.Add(1)
go func(workerID int) {
defer wg.Done()

View File

@@ -752,7 +752,7 @@ func (ret *stateDiff) readProposerLookahead(data *[]byte) error {
// Read the proposer lookahead (2 * SlotsPerEpoch uint64 values)
numProposers := 2 * fieldparams.SlotsPerEpoch
ret.proposerLookahead = make([]uint64, numProposers)
for i := 0; i < numProposers; i++ {
for i := range numProposers {
ret.proposerLookahead[i] = binary.LittleEndian.Uint64((*data)[i*8 : (i+1)*8])
}
*data = (*data)[proposerLookaheadLength:]

View File

@@ -42,7 +42,7 @@ func Test_diffToState(t *testing.T) {
func Test_kmpIndex(t *testing.T) {
intSlice := make([]*int, 10)
for i := 0; i < len(intSlice); i++ {
for i := range intSlice {
intSlice[i] = new(int)
*intSlice[i] = i
}
@@ -544,7 +544,7 @@ func Test_diffToBalances(t *testing.T) {
}
targetBals := target.Balances()
for i := 0; i < len(sourceBals); i++ {
for i := range sourceBals {
require.Equal(t, targetBals[i], sourceBals[i], "balance mismatch at index %d", i)
}
})
@@ -665,7 +665,7 @@ func Test_applyStateDiff(t *testing.T) {
// Test_computeLPS tests the LPS array computation for KMP algorithm
func Test_computeLPS(t *testing.T) {
intSlice := make([]*int, 10)
for i := 0; i < len(intSlice); i++ {
for i := range intSlice {
intSlice[i] = new(int)
*intSlice[i] = i
}
@@ -955,8 +955,7 @@ func BenchmarkGetDiff(b *testing.B) {
source, target, err := getMainnetStates()
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
hdiff, err := Diff(source, target)
b.Log("Diff size:", len(hdiff.StateDiff)+len(hdiff.BalancesDiff)+len(hdiff.ValidatorDiffs))
require.NoError(b, err)
@@ -971,8 +970,8 @@ func BenchmarkApplyDiff(b *testing.B) {
require.NoError(b, err)
hdiff, err := Diff(source, target)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
source, err = ApplyDiff(b.Context(), source, hdiff)
require.NoError(b, err)
}
@@ -998,7 +997,7 @@ func BenchmarkDiffCreation(b *testing.B) {
_ = target.SetValidators(validators)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := Diff(source, target)
if err != nil {
b.Fatal(err)
@@ -1026,7 +1025,7 @@ func BenchmarkDiffApplication(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
// Need fresh source for each iteration
freshSource := source.Copy()
_, err := ApplyDiff(ctx, freshSource, diff)
@@ -1049,8 +1048,7 @@ func BenchmarkSerialization(b *testing.B) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = hdiff.serialize()
}
}
@@ -1067,8 +1065,7 @@ func BenchmarkDeserialization(b *testing.B) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := newHdiff(diff)
if err != nil {
b.Fatal(err)
@@ -1093,7 +1090,7 @@ func BenchmarkBalanceDiff(b *testing.B) {
_ = target.SetBalances(balances)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := diffToBalances(source, target)
if err != nil {
b.Fatal(err)
@@ -1123,7 +1120,7 @@ func BenchmarkValidatorDiff(b *testing.B) {
_ = target.SetValidators(validators)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := diffToVals(source, target)
if err != nil {
b.Fatal(err)
@@ -1172,7 +1169,7 @@ func BenchmarkKMPAlgorithm(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_ = kmpIndex(len(pattern), text, intEquals)
}
})
@@ -1201,7 +1198,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
name: "balance_changes",
modifier: func(target state.BeaconState) {
balances := target.Balances()
for i := 0; i < 10; i++ {
for i := range 10 {
if i < len(balances) {
balances[i] += 1000
}
@@ -1213,7 +1210,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
name: "validator_changes",
modifier: func(target state.BeaconState) {
validators := target.Validators()
for i := 0; i < 10; i++ {
for i := range 10 {
if i < len(validators) {
validators[i].EffectiveBalance += 1000
}
@@ -1235,7 +1232,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
diff, err := Diff(source, testTarget)
if err != nil {
b.Fatal(err)
@@ -1262,7 +1259,7 @@ func BenchmarkMemoryUsage(b *testing.B) {
// Modify some data
validators := target.Validators()
for i := 0; i < 25; i++ {
for i := range 25 {
if i < len(validators) {
validators[i].EffectiveBalance += 1000
}
@@ -1270,9 +1267,8 @@ func BenchmarkMemoryUsage(b *testing.B) {
_ = target.SetValidators(validators)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
diff, err := Diff(source, target)
if err != nil {
b.Fatal(err)