lint: enable intrange linter (#7331)

Enable intrange linter to enforce modern Go range syntax over
traditional for loops, by converting:

for i := 0; i < n; i++

to:

for i := range n

Adding type conversions where needed for compatibility
with existing uint64 parameters.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-05-29 03:50:55 +03:00
committed by GitHub
parent b3acbe5046
commit 19a6ae4983
47 changed files with 82 additions and 81 deletions

View File

@@ -38,7 +38,7 @@ func New(size int) *Cache {
c := &Cache{}
// Initialize all the shards
for i := 0; i < shardSize; i++ {
for i := range shardSize {
c.shards[i] = newShard(ssize)
}
return c

View File

@@ -13,10 +13,10 @@ func TestCacheAddAndGet(t *testing.T) {
t.Fatal("Failed to find inserted record")
}
for i := 0; i < N; i++ {
for i := range N {
c.Add(uint64(i), 1)
}
for i := 0; i < N; i++ {
for i := range N {
c.Add(uint64(i), 1)
if c.Len() != N {
t.Fatal("A item was unnecessarily evicted from the cache")
@@ -45,7 +45,7 @@ func TestCacheLen(t *testing.T) {
func TestCacheSharding(t *testing.T) {
c := New(shardSize)
for i := 0; i < shardSize*2; i++ {
for i := range shardSize * 2 {
c.Add(uint64(i), 1)
}
for i, s := range c.shards {
@@ -58,7 +58,7 @@ func TestCacheSharding(t *testing.T) {
func TestCacheWalk(t *testing.T) {
c := New(10)
exp := make([]int, 10*2)
for i := 0; i < 10*2; i++ {
for i := range 10 * 2 {
c.Add(uint64(i), 1)
exp[i] = 1
}
@@ -78,7 +78,7 @@ func BenchmarkCache(b *testing.B) {
b.ReportAllocs()
c := New(4)
for n := 0; n < b.N; n++ {
for range b.N {
c.Add(1, 1)
c.Get(1)
}

View File

@@ -26,11 +26,11 @@ func TestAddEvict(t *testing.T) {
const size = 1024
s := newShard(size)
for i := uint64(0); i < size; i++ {
s.Add(i, 1)
for i := range size {
s.Add(uint64(i), 1)
}
for i := uint64(0); i < size; i++ {
s.Add(i, 1)
for i := range size {
s.Add(uint64(i), 1)
if s.Len() != size {
t.Fatal("A item was unnecessarily evicted from the cache")
}
@@ -86,7 +86,7 @@ func TestShardLenEvict(t *testing.T) {
// Make sure we don't accidentally evict an element when
// we the key is already stored.
for i := 0; i < 4; i++ {
for range 4 {
s.Add(5, 1)
if l := s.Len(); l != 4 {
t.Fatalf("Shard size should %d, got %d", 4, l)
@@ -96,12 +96,12 @@ func TestShardLenEvict(t *testing.T) {
func TestShardEvictParallel(t *testing.T) {
s := newShard(shardSize)
for i := uint64(0); i < shardSize; i++ {
s.Add(i, struct{}{})
for i := range shardSize {
s.Add(uint64(i), struct{}{})
}
start := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < shardSize; i++ {
for range shardSize {
wg.Add(1)
go func() {
<-start
@@ -119,7 +119,7 @@ func TestShardEvictParallel(t *testing.T) {
func BenchmarkShard(b *testing.B) {
s := newShard(shardSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := range b.N {
k := uint64(i) % shardSize * 2
s.Add(k, 1)
s.Get(k)

View File

@@ -71,7 +71,7 @@ func Reverse(nets []string) []string {
nearest := (bits - ones) + mod
offset := 0
var end bool
for i := 0; i < nearest/sizeDigit; i++ {
for range nearest / sizeDigit {
offset, end = dns.NextLabel(r, offset)
if end {
break

View File

@@ -28,7 +28,7 @@ func NewServer(f dns.HandlerFunc) *Server {
s1 := &dns.Server{} // udp
s2 := &dns.Server{} // tcp
for i := 0; i < 5; i++ { // 5 attempts
for range 5 { // 5 attempts
s2.Listener, _ = reuseport.Listen("tcp", ":0")
if s2.Listener == nil {
continue

View File

@@ -43,7 +43,7 @@ func IsReverse(name string) int {
}
func reverse(slice []string) string {
for i := 0; i < len(slice)/2; i++ {
for i := range len(slice) / 2 {
j := len(slice) - i - 1
slice[i], slice[j] = slice[j], slice[i]
}
@@ -58,12 +58,12 @@ func reverse(slice []string) string {
// b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2
// is reversed to 2001:db8::567:89ab
func reverse6(slice []string) string {
for i := 0; i < len(slice)/2; i++ {
for i := range len(slice) / 2 {
j := len(slice) - i - 1
slice[i], slice[j] = slice[j], slice[i]
}
slice6 := []string{}
for i := 0; i < len(slice)/4; i++ {
for i := range len(slice) / 4 {
slice6 = append(slice6, strings.Join(slice[i*4:i*4+4], ""))
}
ip := net.ParseIP(strings.Join(slice6, ":")).To16()

View File

@@ -63,7 +63,7 @@ func BenchmarkMinimalTTL(b *testing.B) {
mt, _ := response.Typify(m, utc)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
dur := MinimalTTL(m, mt)
if dur != 1000*time.Second {
b.Fatalf("Wrong MinimalTTL %d, expected %d", dur, 1000*time.Second)

View File

@@ -242,7 +242,7 @@ func TestDial_MultipleCallsAfterStop(t *testing.T) {
tr.Stop()
time.Sleep(50 * time.Millisecond)
for i := 0; i < 3; i++ {
for i := range 3 {
_, _, err := tr.Dial("udp")
if err == nil {
t.Errorf("Attempt %d: %s: %s", i+1, testMsgExpectedError, testMsgUnexpectedNilError)

View File

@@ -47,7 +47,7 @@ func TestIntDeterministic(t *testing.T) {
r2 := New(seed)
// They should produce the same sequence
for i := 0; i < 10; i++ {
for i := range 10 {
val1 := r1.Int()
val2 := r2.Int()
if val1 != val2 {
@@ -123,11 +123,11 @@ func TestConcurrentAccess(t *testing.T) {
errors := make(chan error, numGoroutines*numOperations)
// Test concurrent Int() calls
for i := 0; i < numGoroutines; i++ {
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numOperations; j++ {
for range numOperations {
val := r.Int()
if val < 0 {
errors <- nil
@@ -137,11 +137,11 @@ func TestConcurrentAccess(t *testing.T) {
}
// Test concurrent Perm() calls
for i := 0; i < numGoroutines; i++ {
for range numGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numOperations; j++ {
for range numOperations {
perm := r.Perm(5)
if len(perm) != 5 {
errors <- nil
@@ -172,11 +172,11 @@ func TestConcurrentMixedOperations(t *testing.T) {
errors := make(chan error, numGoroutines*numOperations)
// Mix of Int() and Perm() operations running concurrently
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < numOperations; j++ {
for j := range numOperations {
if j%2 == 0 {
val := r.Int()
if val < 0 {

View File

@@ -276,7 +276,7 @@ func BenchmarkReplacer(b *testing.B) {
b.ReportAllocs()
replacer := New()
for i := 0; i < b.N; i++ {
for range b.N {
replacer.Replace(context.TODO(), state, nil, "{type} {name} {size}")
}
}
@@ -297,13 +297,13 @@ func BenchmarkReplacer_CommonLogFormat(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
replacer.Replace(ctxt, state, w, CommonLogFormat)
}
}
func BenchmarkParseFormat(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
parseFormat(CommonLogFormat)
}
}

View File

@@ -63,7 +63,7 @@ func TestDoDupSuppress(t *testing.T) {
const n = 10
var wg sync.WaitGroup
for i := 0; i < n; i++ {
for range n {
wg.Add(1)
go func() {
v, err := g.Do(1, fn)