Skip to content

Commit 6f4bc1f

Browse files
Merge pull request #2305 from kolyshkin/for-range
Use for range over integers
2 parents 0b4d9cf + 73b194b commit 6f4bc1f

15 files changed

+30
-30
lines changed

containers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func (r *containerStore) load(lockedForWriting bool) (bool, error) {
458458

459459
ids := make(map[string]*Container)
460460

461-
for locationIndex := 0; locationIndex < numContainerLocationIndex; locationIndex++ {
461+
for locationIndex := range numContainerLocationIndex {
462462
location := containerLocationFromIndex(locationIndex)
463463
rpath := r.jsonPath[locationIndex]
464464

@@ -531,7 +531,7 @@ func (r *containerStore) save(saveLocations containerLocations) error {
531531
return err
532532
}
533533
r.lastWrite = lw
534-
for locationIndex := 0; locationIndex < numContainerLocationIndex; locationIndex++ {
534+
for locationIndex := range numContainerLocationIndex {
535535
location := containerLocationFromIndex(locationIndex)
536536
if location&saveLocations == 0 {
537537
continue

drivers/copy/copy_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestCopyDir(t *testing.T) {
7676
}
7777

7878
func randomMode(baseMode int) os.FileMode {
79-
for i := 0; i < 7; i++ {
79+
for i := range 7 {
8080
baseMode = baseMode | (1&rand.Intn(2))<<uint(i)
8181
}
8282
return os.FileMode(baseMode)
@@ -94,15 +94,15 @@ func populateSrcDir(t *testing.T, srcDir string, remainingDepth int) {
9494
aTime := time.Unix(rand.Int63(), 0)
9595
mTime := time.Unix(rand.Int63(), 0)
9696

97-
for i := 0; i < 10; i++ {
97+
for i := range 10 {
9898
dirName := filepath.Join(srcDir, fmt.Sprintf("srcdir-%d", i))
9999
// Owner all bits set
100100
assert.NilError(t, os.Mkdir(dirName, randomMode(0o700)))
101101
populateSrcDir(t, dirName, remainingDepth-1)
102102
assert.NilError(t, system.Chtimes(dirName, aTime, mTime))
103103
}
104104

105-
for i := 0; i < 10; i++ {
105+
for i := range 10 {
106106
fileName := filepath.Join(srcDir, fmt.Sprintf("srcfile-%d", i))
107107
// Owner read bit set
108108
assert.NilError(t, os.WriteFile(fileName, []byte{}, randomMode(0o400)))

drivers/graphtest/graphtest_unix.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
468468
var root string
469469
components := 10
470470

471-
for depth := 0; depth < components; depth++ {
471+
for depth := range components {
472472
base := stringid.GenerateRandomID()
473473
second := stringid.GenerateRandomID()
474474
third := stringid.GenerateRandomID()
@@ -486,7 +486,7 @@ func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
486486
}
487487
paths := []string{}
488488
path := "/"
489-
for i := 0; i < components-1; i++ {
489+
for i := range components - 1 {
490490
path = filepath.Join(path, fmt.Sprintf("subdir%d", i+1))
491491
paths = append(paths, path)
492492
if err = os.Mkdir(filepath.Join(root, path), 0o700); err != nil {
@@ -520,7 +520,7 @@ func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
520520
t.Fatal(err)
521521
}
522522
expectedChanges = []archive.Change{}
523-
for i := 0; i < depth; i++ {
523+
for i := range depth {
524524
expectedChanges = append(expectedChanges, archive.Change{Kind: archive.ChangeModify, Path: paths[i]})
525525
}
526526
expectedChanges = append(expectedChanges, archive.Change{Kind: archive.ChangeDelete, Path: paths[depth]})
@@ -541,7 +541,7 @@ func DriverTestEcho(t testing.TB, drivername string, driverOptions ...string) {
541541
}
542542

543543
expectedChanges = []archive.Change{}
544-
for i := 0; i < depth; i++ {
544+
for i := range depth {
545545
expectedChanges = append(expectedChanges, archive.Change{Kind: archive.ChangeModify, Path: paths[i]})
546546
}
547547
for i := depth; i < components-1; i++ {

layers.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ func (r *layerStore) layersModified() (lockfile.LastWrite, bool, error) {
656656
// If the layers.json file or container-layers.json has been
657657
// modified manually, then we have to reload the storage in
658658
// any case.
659-
for locationIndex := 0; locationIndex < numLayerLocationIndex; locationIndex++ {
659+
for locationIndex := range numLayerLocationIndex {
660660
rpath := r.jsonPath[locationIndex]
661661
if rpath == "" {
662662
continue
@@ -794,7 +794,7 @@ func (r *layerStore) load(lockedForWriting bool) (bool, error) {
794794
layers := []*Layer{}
795795
ids := make(map[string]*Layer)
796796

797-
for locationIndex := 0; locationIndex < numLayerLocationIndex; locationIndex++ {
797+
for locationIndex := range numLayerLocationIndex {
798798
location := layerLocationFromIndex(locationIndex)
799799
rpath := r.jsonPath[locationIndex]
800800
if rpath == "" {
@@ -1031,7 +1031,7 @@ func (r *layerStore) saveLayers(saveLocations layerLocations) error {
10311031
}
10321032
r.lastWrite = lw
10331033

1034-
for locationIndex := 0; locationIndex < numLayerLocationIndex; locationIndex++ {
1034+
for locationIndex := range numLayerLocationIndex {
10351035
location := layerLocationFromIndex(locationIndex)
10361036
if location&saveLocations == 0 {
10371037
continue

layers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestLayerLocationFromIndex(t *testing.T) {
2727

2828
func TestLayerLocationFromIndexAndToIndex(t *testing.T) {
2929
var l layerLocations
30-
for i := 0; i < int(unsafe.Sizeof(l)*8); i++ {
30+
for i := range int(unsafe.Sizeof(l) * 8) {
3131
location := layerLocationFromIndex(i)
3232
index := indexFromLayerLocation(location)
3333
require.Equal(t, i, index)

pkg/archive/archive_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ func TestUntarUstarGnuConflict(t *testing.T) {
786786

787787
func prepareUntarSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) {
788788
fileData := []byte("fooo")
789-
for n := 0; n < numberOfFiles; n++ {
789+
for n := range numberOfFiles {
790790
fileName := fmt.Sprintf("file-%d", n)
791791
if err := os.WriteFile(filepath.Join(targetPath, fileName), fileData, 0o700); err != nil {
792792
return 0, err
@@ -1113,7 +1113,7 @@ func TestTempArchiveCloseMultipleTimes(t *testing.T) {
11131113
if n != 5 {
11141114
t.Fatalf("Expected to read 5 bytes. Read %d instead", n)
11151115
}
1116-
for i := 0; i < 3; i++ {
1116+
for i := range 3 {
11171117
if err = tempArchive.Close(); err != nil {
11181118
t.Fatalf("i=%d. Unexpected error closing temp archive: %v", i, err)
11191119
}

pkg/archive/changes_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func parseDirent(buf []byte, names []nameIno) (consumed int, newnames []nameIno)
297297
continue
298298
}
299299
builder := make([]byte, 0, dirent.Reclen)
300-
for i := 0; i < len(dirent.Name); i++ {
300+
for i := range len(dirent.Name) {
301301
if dirent.Name[i] == 0 {
302302
break
303303
}

pkg/archive/changes_posix_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestHardLinkOrder(t *testing.T) {
4242
t.Fatal(err)
4343
}
4444
for _, name := range names {
45-
for i := 0; i < 5; i++ {
45+
for i := range 5 {
4646
if err := os.Link(path.Join(dest, name), path.Join(dest, fmt.Sprintf("%s.link%d", name, i))); err != nil {
4747
t.Fatal(err)
4848
}

pkg/archive/changes_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func TestChangesDirsMutated(t *testing.T) {
357357
{"/symlinknew", ChangeAdd},
358358
}
359359

360-
for i := 0; i < max(len(changes), len(expectedChanges)); i++ {
360+
for i := range max(len(changes), len(expectedChanges)) {
361361
if i >= len(expectedChanges) {
362362
t.Fatalf("unexpected change %s\n", changes[i].String())
363363
}
@@ -470,7 +470,7 @@ func TestChangesSize(t *testing.T) {
470470
func checkChanges(t *testing.T, expectedChanges, changes []Change) {
471471
sort.Sort(changesByPath(expectedChanges))
472472
sort.Sort(changesByPath(changes))
473-
for i := 0; i < max(len(changes), len(expectedChanges)); i++ {
473+
for i := range max(len(changes), len(expectedChanges)) {
474474
if i >= len(expectedChanges) {
475475
t.Fatalf("unexpected change %s\n", changes[i].String())
476476
}

pkg/chrootarchive/archive_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestChrootUntarWithHugeExcludesList(t *testing.T) {
9494
// 65534 entries of 64-byte strings ~= 4MB of environment space which should overflow
9595
// on most systems when passed via environment or command line arguments
9696
excludes := make([]string, 65534)
97-
for i := 0; i < 65534; i++ {
97+
for i := range 65534 {
9898
excludes[i] = strings.Repeat(fmt.Sprintf("%d", i), 64)
9999
}
100100
options.ExcludePatterns = excludes
@@ -112,7 +112,7 @@ func TestChrootUntarEmptyArchive(t *testing.T) {
112112

113113
func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks bool) (int, error) {
114114
fileData := []byte("fooo")
115-
for n := 0; n < numberOfFiles; n++ {
115+
for n := range numberOfFiles {
116116
fileName := fmt.Sprintf("file-%d", n)
117117
if err := os.WriteFile(filepath.Join(targetPath, fileName), fileData, 0o700); err != nil {
118118
return 0, err
@@ -497,7 +497,7 @@ type slowEmptyTarReader struct {
497497
func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
498498
time.Sleep(100 * time.Millisecond)
499499
count := min(len(p), s.chunkSize)
500-
for i := 0; i < count; i++ {
500+
for i := range count {
501501
p[i] = 0
502502
}
503503
s.offset += count

pkg/chunked/compressor/compressor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (rc *rollingChecksumReader) Read(b []byte) (bool, int, error) {
160160
return false, 0, io.EOF
161161
}
162162

163-
for i := 0; i < len(b); i++ {
163+
for i := range b {
164164
holeLen, n, err := rc.reader.readByte()
165165
if err != nil {
166166
if err == io.EOF {

pkg/chunked/compressor/rollsum_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestSum(t *testing.T) {
6060

6161
end := 500
6262
rs := roll(0, windowSize)
63-
for i := 0; i < end; i++ {
63+
for i := range end {
6464
sumRoll := rs.Digest()
6565
newRoll := roll(i, i+windowSize).Digest()
6666

pkg/ioutils/bytespipe_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) {
9090
for _, c := range cases {
9191
// first pass: write directly to hash
9292
hash := sha1.New()
93-
for i := 0; i < c.iterations*c.writesPerLoop; i++ {
93+
for i := range c.iterations * c.writesPerLoop {
9494
if _, err := hash.Write(testMessage[:writeChunks[i%len(writeChunks)]]); err != nil {
9595
t.Fatal(err)
9696
}
@@ -118,8 +118,8 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) {
118118
close(done)
119119
}()
120120

121-
for i := 0; i < c.iterations; i++ {
122-
for w := 0; w < c.writesPerLoop; w++ {
121+
for i := range c.iterations {
122+
for w := range c.writesPerLoop {
123123
_, err := buf.Write(testMessage[:writeChunks[(i*c.writesPerLoop+w)%len(writeChunks)]])
124124
require.NoError(t, err)
125125
}

pkg/lockfile/lockfile_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func TestLockfileMixedConcurrent(t *testing.T) {
521521
done <- true
522522
}
523523

524-
for i := 0; i < numReaders; i++ {
524+
for i := range numReaders {
525525
go reader(&counter)
526526
// schedule a writer every 2nd iteration
527527
if i%2 == 1 {

pkg/stringutils/stringutils.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func GenerateRandomASCIIString(n int) string {
2424
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
2525
"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "
2626
res := make([]byte, n)
27-
for i := 0; i < n; i++ {
27+
for i := range n {
2828
res[i] = chars[rand.IntN(len(chars))]
2929
}
3030
return string(res)
@@ -83,7 +83,7 @@ func quote(word string, buf *bytes.Buffer) {
8383

8484
buf.WriteString("'")
8585

86-
for i := 0; i < len(word); i++ {
86+
for i := range len(word) {
8787
b := word[i]
8888
if b == '\'' {
8989
// Replace literal ' with a close ', a \', and an open '

0 commit comments

Comments
 (0)