From 73b194b8edd725819c4fba965725257f9e0ca476 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 28 Mar 2025 19:56:51 -0700 Subject: [PATCH] Use for range over integers This form is available since Go 1.22 (see https://tip.golang.org/ref/spec#For_range) and will probably be seen more and more in the new code. Signed-off-by: Kir Kolyshkin --- containers.go | 4 ++-- drivers/copy/copy_test.go | 6 +++--- drivers/graphtest/graphtest_unix.go | 8 ++++---- layers.go | 6 +++--- layers_test.go | 2 +- pkg/archive/archive_test.go | 4 ++-- pkg/archive/changes_linux.go | 2 +- pkg/archive/changes_posix_test.go | 2 +- pkg/archive/changes_test.go | 4 ++-- pkg/chrootarchive/archive_test.go | 6 +++--- pkg/chunked/compressor/compressor.go | 2 +- pkg/chunked/compressor/rollsum_test.go | 2 +- pkg/ioutils/bytespipe_test.go | 6 +++--- pkg/lockfile/lockfile_test.go | 2 +- pkg/stringutils/stringutils.go | 4 ++-- 15 files changed, 30 insertions(+), 30 deletions(-) diff --git a/containers.go b/containers.go index 143fde297..58864be17 100644 --- a/containers.go +++ b/containers.go @@ -458,7 +458,7 @@ func (r *containerStore) load(lockedForWriting bool) (bool, error) { ids := make(map[string]*Container) - for locationIndex := 0; locationIndex < numContainerLocationIndex; locationIndex++ { + for locationIndex := range numContainerLocationIndex { location := containerLocationFromIndex(locationIndex) rpath := r.jsonPath[locationIndex] @@ -531,7 +531,7 @@ func (r *containerStore) save(saveLocations containerLocations) error { return err } r.lastWrite = lw - for locationIndex := 0; locationIndex < numContainerLocationIndex; locationIndex++ { + for locationIndex := range numContainerLocationIndex { location := containerLocationFromIndex(locationIndex) if location&saveLocations == 0 { continue diff --git a/drivers/copy/copy_test.go b/drivers/copy/copy_test.go index 959a6b172..6bcc90920 100644 --- a/drivers/copy/copy_test.go +++ b/drivers/copy/copy_test.go @@ -76,7 +76,7 @@ func TestCopyDir(t *testing.T) { } func randomMode(baseMode int) os.FileMode { - for i := 0; i < 7; i++ { + for i := range 7 { baseMode = baseMode | (1&rand.Intn(2))<= len(expectedChanges) { t.Fatalf("unexpected change %s\n", changes[i].String()) } @@ -470,7 +470,7 @@ func TestChangesSize(t *testing.T) { func checkChanges(t *testing.T, expectedChanges, changes []Change) { sort.Sort(changesByPath(expectedChanges)) sort.Sort(changesByPath(changes)) - for i := 0; i < max(len(changes), len(expectedChanges)); i++ { + for i := range max(len(changes), len(expectedChanges)) { if i >= len(expectedChanges) { t.Fatalf("unexpected change %s\n", changes[i].String()) } diff --git a/pkg/chrootarchive/archive_test.go b/pkg/chrootarchive/archive_test.go index f562126b4..e90400e10 100644 --- a/pkg/chrootarchive/archive_test.go +++ b/pkg/chrootarchive/archive_test.go @@ -94,7 +94,7 @@ func TestChrootUntarWithHugeExcludesList(t *testing.T) { // 65534 entries of 64-byte strings ~= 4MB of environment space which should overflow // on most systems when passed via environment or command line arguments excludes := make([]string, 65534) - for i := 0; i < 65534; i++ { + for i := range 65534 { excludes[i] = strings.Repeat(fmt.Sprintf("%d", i), 64) } options.ExcludePatterns = excludes @@ -112,7 +112,7 @@ func TestChrootUntarEmptyArchive(t *testing.T) { func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks bool) (int, error) { fileData := []byte("fooo") - for n := 0; n < numberOfFiles; n++ { + for n := range numberOfFiles { fileName := fmt.Sprintf("file-%d", n) if err := os.WriteFile(filepath.Join(targetPath, fileName), fileData, 0o700); err != nil { return 0, err @@ -497,7 +497,7 @@ type slowEmptyTarReader struct { func (s *slowEmptyTarReader) Read(p []byte) (int, error) { time.Sleep(100 * time.Millisecond) count := min(len(p), s.chunkSize) - for i := 0; i < count; i++ { + for i := range count { p[i] = 0 } s.offset += count diff --git a/pkg/chunked/compressor/compressor.go b/pkg/chunked/compressor/compressor.go index 7a17fd750..0de063a24 100644 --- a/pkg/chunked/compressor/compressor.go +++ b/pkg/chunked/compressor/compressor.go @@ -160,7 +160,7 @@ func (rc *rollingChecksumReader) Read(b []byte) (bool, int, error) { return false, 0, io.EOF } - for i := 0; i < len(b); i++ { + for i := range b { holeLen, n, err := rc.reader.readByte() if err != nil { if err == io.EOF { diff --git a/pkg/chunked/compressor/rollsum_test.go b/pkg/chunked/compressor/rollsum_test.go index b2df6667e..6008910b3 100644 --- a/pkg/chunked/compressor/rollsum_test.go +++ b/pkg/chunked/compressor/rollsum_test.go @@ -60,7 +60,7 @@ func TestSum(t *testing.T) { end := 500 rs := roll(0, windowSize) - for i := 0; i < end; i++ { + for i := range end { sumRoll := rs.Digest() newRoll := roll(i, i+windowSize).Digest() diff --git a/pkg/ioutils/bytespipe_test.go b/pkg/ioutils/bytespipe_test.go index cdd8fdd51..0f887c546 100644 --- a/pkg/ioutils/bytespipe_test.go +++ b/pkg/ioutils/bytespipe_test.go @@ -90,7 +90,7 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) { for _, c := range cases { // first pass: write directly to hash hash := sha1.New() - for i := 0; i < c.iterations*c.writesPerLoop; i++ { + for i := range c.iterations * c.writesPerLoop { if _, err := hash.Write(testMessage[:writeChunks[i%len(writeChunks)]]); err != nil { t.Fatal(err) } @@ -118,8 +118,8 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) { close(done) }() - for i := 0; i < c.iterations; i++ { - for w := 0; w < c.writesPerLoop; w++ { + for i := range c.iterations { + for w := range c.writesPerLoop { _, err := buf.Write(testMessage[:writeChunks[(i*c.writesPerLoop+w)%len(writeChunks)]]) require.NoError(t, err) } diff --git a/pkg/lockfile/lockfile_test.go b/pkg/lockfile/lockfile_test.go index 4f45916b6..d8513575f 100644 --- a/pkg/lockfile/lockfile_test.go +++ b/pkg/lockfile/lockfile_test.go @@ -521,7 +521,7 @@ func TestLockfileMixedConcurrent(t *testing.T) { done <- true } - for i := 0; i < numReaders; i++ { + for i := range numReaders { go reader(&counter) // schedule a writer every 2nd iteration if i%2 == 1 { diff --git a/pkg/stringutils/stringutils.go b/pkg/stringutils/stringutils.go index f63c3e444..303a7291f 100644 --- a/pkg/stringutils/stringutils.go +++ b/pkg/stringutils/stringutils.go @@ -24,7 +24,7 @@ func GenerateRandomASCIIString(n int) string { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` " res := make([]byte, n) - for i := 0; i < n; i++ { + for i := range n { res[i] = chars[rand.IntN(len(chars))] } return string(res) @@ -83,7 +83,7 @@ func quote(word string, buf *bytes.Buffer) { buf.WriteString("'") - for i := 0; i < len(word); i++ { + for i := range len(word) { b := word[i] if b == '\'' { // Replace literal ' with a close ', a \', and an open '