all: use Go 1.22 range-over-int

Updates #11058

Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2024-04-16 13:15:13 -07:00
committed by Brad Fitzpatrick
parent 068db1f972
commit 7c1d6e35a5
143 changed files with 280 additions and 282 deletions

View File

@@ -201,7 +201,7 @@ func AssertStructUnchanged(t *types.Struct, tname, ctx string, it *ImportTracker
w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
w("var _%s%sNeedsRegeneration = %s(struct {", tname, ctx, tname)
for i := 0; i < t.NumFields(); i++ {
for i := range t.NumFields() {
st := t.Field(i)
fname := st.Name()
ft := t.Field(i).Type()
@@ -253,7 +253,7 @@ func ContainsPointers(typ types.Type) bool {
case *types.Slice:
return true
case *types.Struct:
for i := 0; i < ft.NumFields(); i++ {
for i := range ft.NumFields() {
if ContainsPointers(ft.Field(i).Type()) {
return true
}

View File

@@ -78,7 +78,7 @@ func TestDecoder(t *testing.T) {
dec := func(n int) *Decoder {
// Make a buffer of the exact size that consists of 0xff bytes
buf := make([]byte, n)
for i := 0; i < n; i++ {
for i := range n {
buf[i] = 0xff
}
@@ -108,7 +108,7 @@ func TestDecoder(t *testing.T) {
dec := func(n int) *Decoder {
// Make a buffer that's too small and contains arbitrary bytes
buf := make([]byte, n-1)
for i := 0; i < n-1; i++ {
for i := range n - 1 {
buf[i] = 0xAD
}

View File

@@ -47,7 +47,7 @@
//
// var big *Item = ... // some large data structure that is slow to hash
// var manyBig []*Item
// for i := 0; i < 1000; i++ {
// for i := range 1000 {
// manyBig = append(manyBig, &big)
// }
// deephash.Hash(manyBig)
@@ -208,7 +208,7 @@ type Sum struct {
}
func (s1 *Sum) xor(s2 Sum) {
for i := 0; i < sha256.Size; i++ {
for i := range sha256.Size {
s1.sum[i] ^= s2.sum[i]
}
}
@@ -492,7 +492,7 @@ func makeArrayHasher(t reflect.Type) typeHasherFunc {
nb := t.Elem().Size() // byte size of each array element
return func(h *hasher, p pointer) {
once.Do(init)
for i := 0; i < n; i++ {
for i := range n {
hashElem(h, p.arrayIndex(i, nb))
}
}
@@ -545,7 +545,7 @@ func makeSliceHasher(t reflect.Type) typeHasherFunc {
h.HashUint8(1) // indicates visiting slice
n := p.sliceLen()
h.HashUint64(uint64(n))
for i := 0; i < n; i++ {
for i := range n {
pe := pa.arrayIndex(i, nb)
hashElem(h, pe)
}

View File

@@ -202,7 +202,7 @@ func TestDeepHash(t *testing.T) {
v := getVal()
hash1 := Hash(v)
t.Logf("hash: %v", hash1)
for i := 0; i < 20; i++ {
for range 20 {
v := getVal()
hash2 := Hash(v)
if hash1 != hash2 {
@@ -760,7 +760,7 @@ var sink Sum
func BenchmarkHash(b *testing.B) {
b.ReportAllocs()
v := getVal()
for i := 0; i < b.N; i++ {
for range b.N {
sink = Hash(v)
}
}
@@ -809,14 +809,14 @@ var filterRules = []tailcfg.FilterRule{
func BenchmarkHashPacketFilter(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
sink = Hash(&filterRules)
}
}
func TestHashMapAcyclic(t *testing.T) {
m := map[int]string{}
for i := 0; i < 100; i++ {
for i := range 100 {
m[i] = fmt.Sprint(i)
}
got := map[string]bool{}
@@ -824,7 +824,7 @@ func TestHashMapAcyclic(t *testing.T) {
hb := &hashBuffer{Hash: sha256.New()}
hash := lookupTypeHasher(reflect.TypeFor[map[int]string]())
for i := 0; i < 20; i++ {
for range 20 {
va := reflect.ValueOf(&m).Elem()
hb.Reset()
h := new(hasher)
@@ -862,7 +862,7 @@ func TestPrintArray(t *testing.T) {
func BenchmarkHashMapAcyclic(b *testing.B) {
b.ReportAllocs()
m := map[int]string{}
for i := 0; i < 100; i++ {
for i := range 100 {
m[i] = fmt.Sprint(i)
}
@@ -873,7 +873,7 @@ func BenchmarkHashMapAcyclic(b *testing.B) {
h := new(hasher)
h.Block512.Hash = hb
for i := 0; i < b.N; i++ {
for range b.N {
h.Reset()
hash(h, pointerOf(va.Addr()))
}
@@ -883,14 +883,14 @@ func BenchmarkTailcfgNode(b *testing.B) {
b.ReportAllocs()
node := new(tailcfg.Node)
for i := 0; i < b.N; i++ {
for range b.N {
sink = Hash(node)
}
}
func TestExhaustive(t *testing.T) {
seen := make(map[Sum]bool)
for i := 0; i < 100000; i++ {
for i := range 100000 {
s := Hash(&i)
if seen[s] {
t.Fatalf("hash collision %v", i)
@@ -971,7 +971,7 @@ func BenchmarkHashArray(b *testing.B) {
}
x := &T{X: [32]byte{1: 1, 2: 2, 3: 3, 4: 4}}
for i := 0; i < b.N; i++ {
for range b.N {
sink = Hash(x)
}
}
@@ -1134,7 +1134,7 @@ func BenchmarkAppendTo(b *testing.B) {
hashBuf := make([]byte, 0, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
hashBuf = h.AppendTo(hashBuf[:0])
}
}

View File

@@ -40,7 +40,7 @@ func ToFQDN(s string) (FQDN, error) {
}
st := 0
for i := 0; i < len(s); i++ {
for i := range len(s) {
if s[i] != '.' {
continue
}

View File

@@ -224,7 +224,7 @@ func BenchmarkToFQDN(b *testing.B) {
for _, test := range tests {
b.Run(test, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
sinkFQDN, _ = ToFQDN(test)
}
})

View File

@@ -113,7 +113,7 @@ func TestSafeFuncSlowOnSlow(t *testing.T) {
slowValue.Store(v)
})
for i := 0; i < 10; i++ {
for range 10 {
if got := f.Value(); got != nil {
t.Fatalf("got value=%v; want nil", got)
}

View File

@@ -23,21 +23,21 @@ func TestNewUUID(t *testing.T) {
func BenchmarkBasic(b *testing.B) {
b.Run("NewUUID", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
NewUUID()
}
})
b.Run("uuid.New-unpooled", func(b *testing.B) {
uuid.DisableRandPool()
for i := 0; i < b.N; i++ {
for range b.N {
uuid.New()
}
})
b.Run("uuid.New-pooled", func(b *testing.B) {
uuid.EnableRandPool()
for i := 0; i < b.N; i++ {
for range b.N {
uuid.New()
}
})

View File

@@ -46,7 +46,7 @@ type hasher interface {
}
func hashSuite(h hasher) {
for i := 0; i < 10; i++ {
for i := range 10 {
for j := 0; j < 10; j++ {
h.HashUint8(0x01)
h.HashUint8(0x23)
@@ -133,7 +133,7 @@ func Fuzz(f *testing.F) {
c := qt.New(t)
execute := func(h hasher, r *rand.Rand) {
for i := 0; i < r.Intn(256); i++ {
for range r.Intn(256) {
switch r.Intn(5) {
case 0:
n := uint8(r.Uint64())
@@ -186,7 +186,7 @@ func Benchmark(b *testing.B) {
b.Run("Hash", func(b *testing.B) {
b.ReportAllocs()
h := must.Get(New512(sha256.New()))
for i := 0; i < b.N; i++ {
for range b.N {
h.Reset()
hashSuite(h)
h.Sum(sum[:0])
@@ -195,7 +195,7 @@ func Benchmark(b *testing.B) {
b.Run("Naive", func(b *testing.B) {
b.ReportAllocs()
h := newNaive()
for i := 0; i < b.N; i++ {
for range b.N {
h.Reset()
hashSuite(h)
h.Sum(sum[:0])

View File

@@ -49,7 +49,7 @@ func BenchmarkUnmarshal(b *testing.B) {
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
Unmarshal(j, &m)
}
}
@@ -58,7 +58,7 @@ func BenchmarkStdUnmarshal(b *testing.B) {
var m any
j := []byte("5")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
json.Unmarshal(j, &m)
}
}

View File

@@ -177,7 +177,7 @@ func TestDumpHTML(t *testing.T) {
func allowed(t *testing.T, l *Limiter[string], key string, count int, now time.Time) {
t.Helper()
for i := 0; i < count; i++ {
for i := range count {
if !l.allow(key, now) {
toks, ok := l.tokensForTest(key)
t.Errorf("after %d times: allow(%q, %q) = false, want true (%d tokens available, in cache = %v)", i, key, now, toks, ok)
@@ -187,7 +187,7 @@ func allowed(t *testing.T, l *Limiter[string], key string, count int, now time.T
func denied(t *testing.T, l *Limiter[string], key string, count int, now time.Time) {
t.Helper()
for i := 0; i < count; i++ {
for i := range count {
if l.allow(key, now) {
toks, ok := l.tokensForTest(key)
t.Errorf("after %d times: allow(%q, %q) = true, want false (%d tokens available, in cache = %v)", i, key, now, toks, ok)

View File

@@ -77,7 +77,7 @@ func TestStressEvictions(t *testing.T) {
MaxEntries: cacheSize,
}
for i := 0; i < numProbes; i++ {
for range numProbes {
v := vals[rand.Intn(len(vals))]
c.Set(v, true)
if l := c.Len(); l > cacheSize {
@@ -106,7 +106,7 @@ func TestStressBatchedEvictions(t *testing.T) {
c := Cache[uint64, bool]{}
for i := 0; i < numProbes; i++ {
for range numProbes {
v := vals[rand.Intn(len(vals))]
c.Set(v, true)
if c.Len() == cacheSizeMax {
@@ -127,7 +127,7 @@ func TestLRUStress(t *testing.T) {
maxSize = 500
numProbes = 5_000
)
for i := 0; i < numProbes; i++ {
for range numProbes {
n := rand.Intn(maxSize * 2)
op := rand.Intn(4)
switch op {
@@ -230,7 +230,7 @@ func BenchmarkLRU(b *testing.B) {
c := Cache[int, bool]{MaxEntries: lruSize}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
k := rand.Intn(maxval)
if !c.Get(k) {
c.Set(k, true)

View File

@@ -111,7 +111,7 @@ var sink error
func BenchmarkEmpty(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
sink = multierr.New(nil, nil, nil, multierr.Error{})
}
}
@@ -119,7 +119,7 @@ func BenchmarkEmpty(b *testing.B) {
func BenchmarkNonEmpty(b *testing.B) {
merr := multierr.New(io.ErrShortBuffer, io.ErrNoProgress)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
sink = multierr.New(io.ErrUnexpectedEOF, merr, io.ErrClosedPipe)
}
}

View File

@@ -91,7 +91,7 @@ func AppendSliceElem[K ~string, S []E, E any](m map[K]S, k K, vs ...E) {
}
func isLowerASCII(s string) bool {
for i := 0; i < len(s); i++ {
for i := range len(s) {
if c := s[i]; c >= utf8.RuneSelf || ('A' <= c && c <= 'Z') {
return false
}

View File

@@ -112,13 +112,13 @@ func Benchmark(b *testing.B) {
b.Run("Get", func(b *testing.B) {
b.Run("Naive", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
testSink = testMap[strings.ToLower(key)]
}
})
b.Run("NoCase", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
testSink = Get(testMap, key)
}
})
@@ -127,7 +127,7 @@ func Benchmark(b *testing.B) {
b.Run("Naive", func(b *testing.B) {
b.ReportAllocs()
testMap[strings.ToLower(key)] = testValue
for i := 0; i < b.N; i++ {
for range b.N {
testMap[strings.ToLower(key)] = testValue
}
xmaps.Clear(testMap)
@@ -135,7 +135,7 @@ func Benchmark(b *testing.B) {
b.Run("NoCase", func(b *testing.B) {
b.ReportAllocs()
Set(testMap, key, testValue)
for i := 0; i < b.N; i++ {
for range b.N {
Set(testMap, key, testValue)
}
xmaps.Clear(testMap)
@@ -144,13 +144,13 @@ func Benchmark(b *testing.B) {
b.Run("Delete", func(b *testing.B) {
b.Run("Naive", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
delete(testMap, strings.ToLower(key))
}
})
b.Run("NoCase", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
Delete(testMap, key)
}
})

View File

@@ -31,7 +31,7 @@ func TestOwnerOfPID(t *testing.T) {
func TestNotFoundError(t *testing.T) {
// Try a bunch of times to stumble upon a pid that doesn't exist...
const tries = 50
for i := 0; i < tries; i++ {
for range tries {
_, err := OwnerOfPID(rand.Intn(1e9))
if err == ErrNotImplemented {
t.Skip(err)

View File

@@ -87,7 +87,7 @@ func (rh *Race[T]) Start(ctx context.Context) (T, error) {
// For each possible result, get it off the channel.
var errs []error
for i := 0; i < 2; i++ {
for range 2 {
res := <-rh.results
// If this was an error, store it and hope that the other

View File

@@ -51,7 +51,7 @@ func (rb *RingBuffer[T]) GetAll() []T {
rb.mu.Lock()
defer rb.mu.Unlock()
out := make([]T, len(rb.buf))
for i := 0; i < len(rb.buf); i++ {
for i := range len(rb.buf) {
x := (rb.pos + i) % rb.max
out[i] = rb.buf[x]
}

View File

@@ -12,7 +12,7 @@ func TestRingBuffer(t *testing.T) {
const numItems = 10
rb := New[int](numItems)
for i := 0; i < numItems-1; i++ {
for i := range numItems - 1 {
rb.Add(i)
}

View File

@@ -67,7 +67,7 @@ func (ss *Slice[T]) Add(vs ...T) {
// AddSlice adds all elements in vs to the set.
func (ss *Slice[T]) AddSlice(vs views.Slice[T]) {
for i := 0; i < vs.Len(); i++ {
for i := range vs.Len() {
ss.Add(vs.At(i))
}
}

View File

@@ -69,7 +69,7 @@ func TestDoDupSuppress(t *testing.T) {
const n = 10
wg1.Add(1)
for i := 0; i < n; i++ {
for range n {
wg1.Add(1)
wg2.Add(1)
go func() {
@@ -167,7 +167,7 @@ func TestPanicDo(t *testing.T) {
waited := int32(n)
panicCount := int32(0)
done := make(chan struct{})
for i := 0; i < n; i++ {
for range n {
go func() {
defer func() {
if err := recover(); err != nil {
@@ -204,7 +204,7 @@ func TestGoexitDo(t *testing.T) {
const n = 5
waited := int32(n)
done := make(chan struct{})
for i := 0; i < n; i++ {
for range n {
go func() {
var err error
defer func() {

View File

@@ -38,7 +38,7 @@ func TestInterleave(t *testing.T) {
func BenchmarkInterleave(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
Interleave(
[]int{1, 2, 3},
[]int{9, 8, 7},
@@ -48,7 +48,7 @@ func BenchmarkInterleave(b *testing.B) {
func TestShuffle(t *testing.T) {
var sl []int
for i := 0; i < 100; i++ {
for i := range 100 {
sl = append(sl, i)
}

View File

@@ -145,7 +145,7 @@ func PickParams(err, probability float64) (hashes, buckets int) {
}
func (cms *CountMinSketch) init(hashes, buckets int) {
for i := 0; i < hashes; i++ {
for range hashes {
cms.hashes = append(cms.hashes, maphash.MakeSeed())
}

View File

@@ -49,7 +49,7 @@ func TestTopK(t *testing.T) {
}, 4, 1000)
// Add the first 10 integers with counts equal to 2x their value
for i := 0; i < 10; i++ {
for i := range 10 {
topk.AddN(i, uint64(i*2))
}
@@ -82,7 +82,7 @@ func BenchmarkCountMinSketch(b *testing.B) {
b.ReportAllocs()
var enc [8]byte
for i := 0; i < b.N; i++ {
for i := range b.N {
binary.LittleEndian.PutUint64(enc[:], uint64(i))
cms.Add(enc[:])
}
@@ -104,7 +104,7 @@ func BenchmarkTopK(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for i := range b.N {
topk.Add(i)
}
out = topk.AppendTop(out[:0]) // should not allocate

View File

@@ -94,7 +94,7 @@ func benchmark(b *testing.B, size int64, reset func(s []byte)) {
b.SetBytes(size)
s := make([]byte, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
s = s[:size]
reset(s)
uniq.ModifySlice(&s)

View File

@@ -25,7 +25,7 @@ import (
var (
// ErrDefunctProcess is returned by (*UniqueProcess).AsRestartableProcess
// when the process no longer exists.
ErrDefunctProcess = errors.New("process is defunct")
ErrDefunctProcess = errors.New("process is defunct")
// ErrProcessNotRestartable is returned by (*UniqueProcess).AsRestartableProcess
// when the process has previously indicated that it must not be restarted
// during a patch/upgrade.

View File

@@ -67,7 +67,7 @@ func testRestartableProcessesImpl(N int, t *testing.T) {
const binary = "testrestartableprocesses"
fq := pathToTestProg(t, binary)
for i := 0; i < N; i++ {
for range N {
startTestProg(t, binary, "RestartableProcess")
}
t.Cleanup(func() {

View File

@@ -10,7 +10,7 @@ import "os"
var (
cmds = map[string]func(){}
err error
err error
)
func register(name string, f func()) {

View File

@@ -128,7 +128,7 @@ func BenchmarkEncode(b *testing.B) {
b.Run(bb.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(src)))
for i := 0; i < b.N; i++ {
for range b.N {
dst = AppendEncode(dst[:0], src, bb.opts...)
}
})
@@ -153,7 +153,7 @@ func BenchmarkDecode(b *testing.B) {
b.Run(bb.name, func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(len(src)))
for i := 0; i < b.N; i++ {
for range b.N {
dst = must.Get(AppendDecode(dst[:0], src, bb.opts...))
}
})
@@ -164,12 +164,12 @@ func BenchmarkEncodeParallel(b *testing.B) {
numCPU := runtime.NumCPU()
for _, coder := range coders {
dsts = dsts[:0]
for i := 0; i < numCPU; i++ {
for range numCPU {
dsts = append(dsts, coder.appendEncode(nil, src))
}
b.Run(coder.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
var group sync.WaitGroup
for j := 0; j < numCPU; j++ {
group.Add(1)
@@ -189,12 +189,12 @@ func BenchmarkDecodeParallel(b *testing.B) {
for _, coder := range coders {
dsts = dsts[:0]
src := AppendEncode(nil, src)
for i := 0; i < numCPU; i++ {
for range numCPU {
dsts = append(dsts, must.Get(coder.appendDecode(nil, src)))
}
b.Run(coder.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for range b.N {
var group sync.WaitGroup
for j := 0; j < numCPU; j++ {
group.Add(1)