diff --git a/cmd/restic/cmd_backup_integration_test.go b/cmd/restic/cmd_backup_integration_test.go index cfca46ce1..e367a1761 100644 --- a/cmd/restic/cmd_backup_integration_test.go +++ b/cmd/restic/cmd_backup_integration_test.go @@ -154,7 +154,7 @@ func (f *vssDeleteOriginalFS) Lstat(name string) (*fs.ExtendedFileInfo, error) { _, _ = f.FS.Lstat(name) // nuke testdata var err error - for i := 0; i < 3; i++ { + for range 3 { // The CI sometimes runs into "The process cannot access the file because it is being used by another process" errors // thus try a few times to remove the data err = os.RemoveAll(f.testdata) @@ -738,7 +738,7 @@ func TestBackupSkipIfUnchanged(t *testing.T) { testSetupBackupData(t, env) opts := BackupOptions{SkipIfUnchanged: true} - for i := 0; i < 3; i++ { + for range 3 { testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts) testListSnapshots(t, env.gopts, 1) } diff --git a/cmd/restic/cmd_mount_integration_test.go b/cmd/restic/cmd_mount_integration_test.go index 5aff2ef92..0ce0eab8a 100644 --- a/cmd/restic/cmd_mount_integration_test.go +++ b/cmd/restic/cmd_mount_integration_test.go @@ -46,7 +46,7 @@ func snapshotsDirExists(t testing.TB, dir string) bool { // waitForMount blocks (max mountWait * mountSleep) until the subdir // "snapshots" appears in the dir. func waitForMount(t testing.TB, dir string) { - for i := 0; i < mountWait; i++ { + for range mountWait { if snapshotsDirExists(t, dir) { t.Log("mounted directory is ready") return @@ -70,7 +70,7 @@ func testRunMount(t testing.TB, gopts global.Options, dir string, wg *sync.WaitG func testRunUmount(t testing.TB, dir string) { var err error - for i := 0; i < mountWait; i++ { + for range mountWait { if err = systemFuse.Unmount(dir); err == nil { t.Logf("directory %v umounted", dir) return diff --git a/cmd/restic/cmd_restore_integration_test.go b/cmd/restic/cmd_restore_integration_test.go index 303b4e7ca..1eea148d3 100644 --- a/cmd/restic/cmd_restore_integration_test.go +++ b/cmd/restic/cmd_restore_integration_test.go @@ -243,7 +243,7 @@ func TestRestore(t *testing.T) { testRunInit(t, env.gopts) - for i := 0; i < 10; i++ { + for i := range 10 { p := filepath.Join(env.testdata, fmt.Sprintf("foo/bar/testfile%v", i)) rtest.OK(t, os.MkdirAll(filepath.Dir(p), 0755)) rtest.OK(t, appendRandomData(p, uint(rand.Intn(2<<21)))) diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index bac1e11f5..06c5e3592 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -415,7 +415,7 @@ func statsDebugFileType(ctx context.Context, repo restic.Lister, tpe restic.File func statsDebugBlobs(ctx context.Context, repo restic.Repository) ([restic.NumBlobTypes]*sizeHistogram, error) { var hist [restic.NumBlobTypes]*sizeHistogram - for i := 0; i < len(hist); i++ { + for i := range len(hist) { hist[i] = newSizeHistogram(2 * chunker.MaxSize) } diff --git a/cmd/restic/cmd_stats_test.go b/cmd/restic/cmd_stats_test.go index 02d37acd9..300a01ec2 100644 --- a/cmd/restic/cmd_stats_test.go +++ b/cmd/restic/cmd_stats_test.go @@ -24,7 +24,7 @@ func TestSizeHistogramNew(t *testing.T) { func TestSizeHistogramAdd(t *testing.T) { h := newSizeHistogram(42) - for i := uint64(0); i < 45; i++ { + for i := range uint64(45) { h.Add(i) } diff --git a/helpers/build-release-binaries/main.go b/helpers/build-release-binaries/main.go index f2468145d..5a0375200 100644 --- a/helpers/build-release-binaries/main.go +++ b/helpers/build-release-binaries/main.go @@ -215,7 +215,7 @@ func buildTargets(sourceDir, outputDir string, targets map[string][]string) { var wg errgroup.Group ch := make(chan Job) - for i := 0; i < workers; i++ { + for range workers { wg.Go(func() error { for job := range ch { start := time.Now() diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index e3f5474ec..d0ce9c827 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -477,7 +477,7 @@ func TestArchiverSaveFileIncremental(t *testing.T) { data := rtest.Random(23, 512*1024+887898) testfile := filepath.Join(tempdir, "testfile") - for i := 0; i < 3; i++ { + for i := range 3 { appendToFile(t, testfile, data) node, _ := saveFile(t, repo, testfile, fs.Track{FS: fs.NewLocal()}) @@ -984,7 +984,7 @@ func TestArchiverSaveDirIncremental(t *testing.T) { // save the empty directory several times in a row, then have a look if the // archiver did save the same tree several times - for i := 0; i < 5; i++ { + for i := range 5 { testFS := fs.Track{FS: fs.NewLocal()} arch := New(repo, testFS, Options{}) arch.summary = &Summary{} diff --git a/internal/archiver/buffer_test.go b/internal/archiver/buffer_test.go index 1b577fa2d..65263c5a2 100644 --- a/internal/archiver/buffer_test.go +++ b/internal/archiver/buffer_test.go @@ -7,7 +7,7 @@ import ( func TestBufferPoolReuse(t *testing.T) { success := false // retries to avoid flakiness. The test can fail depending on the GC. - for i := 0; i < 100; i++ { + for range 100 { // Test that buffers are actually reused from the pool pool := newBufferPool(1024) @@ -33,7 +33,7 @@ func TestBufferPoolReuse(t *testing.T) { func TestBufferPoolLargeBuffers(t *testing.T) { success := false // retries to avoid flakiness. The test can fail depending on the GC. - for i := 0; i < 100; i++ { + for range 100 { // Test that buffers larger than defaultSize are not returned to pool pool := newBufferPool(1024) buf := pool.Get() diff --git a/internal/archiver/file_saver.go b/internal/archiver/file_saver.go index 48b308e00..f25f3c7b8 100644 --- a/internal/archiver/file_saver.go +++ b/internal/archiver/file_saver.go @@ -45,7 +45,7 @@ func newFileSaver(ctx context.Context, wg *errgroup.Group, uploader restic.BlobS CompleteBlob: func(uint64) {}, } - for i := uint(0); i < fileWorkers; i++ { + for range fileWorkers { wg.Go(func() error { s.worker(ctx, ch) return nil diff --git a/internal/archiver/file_saver_test.go b/internal/archiver/file_saver_test.go index 9be2f0d95..91d2aa11b 100644 --- a/internal/archiver/file_saver_test.go +++ b/internal/archiver/file_saver_test.go @@ -18,7 +18,7 @@ import ( func createTestFiles(t testing.TB, num int) (files []string) { tempdir := test.TempDir(t) - for i := 0; i < num; i++ { + for i := range num { filename := fmt.Sprintf("testfile-%d", i) err := os.WriteFile(filepath.Join(tempdir, filename), []byte(filename), 0600) if err != nil { diff --git a/internal/archiver/tree_saver.go b/internal/archiver/tree_saver.go index 8b38b5eb2..295a4c393 100644 --- a/internal/archiver/tree_saver.go +++ b/internal/archiver/tree_saver.go @@ -29,7 +29,7 @@ func newTreeSaver(ctx context.Context, wg *errgroup.Group, treeWorkers uint, upl errFn: errFn, } - for i := uint(0); i < treeWorkers; i++ { + for range treeWorkers { wg.Go(func() error { return s.worker(ctx, ch) }) diff --git a/internal/archiver/tree_saver_test.go b/internal/archiver/tree_saver_test.go index ed3a148af..d3ba741c2 100644 --- a/internal/archiver/tree_saver_test.go +++ b/internal/archiver/tree_saver_test.go @@ -54,7 +54,7 @@ func TestTreeSaver(t *testing.T) { var results []futureNode - for i := 0; i < 20; i++ { + for i := range 20 { node := &data.Node{ Name: fmt.Sprintf("file-%d", i), } diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index e23455d13..5d539fb22 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -258,7 +258,7 @@ func (be *b2Backend) Stat(ctx context.Context, h backend.Handle) (bi backend.Fil // Remove removes the blob with the given name and type. func (be *b2Backend) Remove(ctx context.Context, h backend.Handle) error { // the retry backend will also repeat the remove method up to 10 times - for i := 0; i < 3; i++ { + for range 3 { obj := be.bucket.Object(be.Filename(h)) var err error diff --git a/internal/backend/cache/backend_test.go b/internal/backend/cache/backend_test.go index 12d4fa028..4f388027b 100644 --- a/internal/backend/cache/backend_test.go +++ b/internal/backend/cache/backend_test.go @@ -238,7 +238,7 @@ func TestErrorBackend(t *testing.T) { wrappedBE := c.Wrap(errBackend, t.Logf) var wg sync.WaitGroup - for i := 0; i < 5; i++ { + for range 5 { wg.Add(1) go loadTest(&wg, wrappedBE) } diff --git a/internal/backend/cache/cache_test.go b/internal/backend/cache/cache_test.go index b9d0b905d..7b56e40dc 100644 --- a/internal/backend/cache/cache_test.go +++ b/internal/backend/cache/cache_test.go @@ -24,7 +24,7 @@ func TestNew(t *testing.T) { stepEnd ) - for step := stepCreate; step < stepEnd; step++ { + for step := range stepEnd { switch step { case stepRmTag: rtest.OK(t, os.Remove(tagFile)) diff --git a/internal/backend/cache/file_test.go b/internal/backend/cache/file_test.go index d26c8cc6a..bfeda4959 100644 --- a/internal/backend/cache/file_test.go +++ b/internal/backend/cache/file_test.go @@ -243,7 +243,7 @@ func TestFileSaveConcurrent(t *testing.T) { Name: id.String(), } - for i := 0; i < nproc/2; i++ { + for range nproc / 2 { g.Go(func() error { return c.save(h, bytes.NewReader(data)) }) // Can't use load because only the main goroutine may call t.Fatal. diff --git a/internal/backend/layout/layout_default.go b/internal/backend/layout/layout_default.go index 6566da5a7..e5ff6a8a7 100644 --- a/internal/backend/layout/layout_default.go +++ b/internal/backend/layout/layout_default.go @@ -72,7 +72,7 @@ func (l *DefaultLayout) Paths() (dirs []string) { if !disablePackSubdirs { // also add subdirs - for i := 0; i < 256; i++ { + for i := range 256 { subdir := hex.EncodeToString([]byte{byte(i)}) dirs = append(dirs, l.join(l.path, defaultLayoutPaths[backend.PackFile], subdir)) } diff --git a/internal/backend/layout/layout_test.go b/internal/backend/layout/layout_test.go index af5105c20..1049439b8 100644 --- a/internal/backend/layout/layout_test.go +++ b/internal/backend/layout/layout_test.go @@ -112,7 +112,7 @@ func TestDefaultLayout(t *testing.T) { filepath.Join(tempdir, "keys"), } - for i := 0; i < 256; i++ { + for i := range 256 { want = append(want, filepath.Join(tempdir, "data", fmt.Sprintf("%02x", i))) } diff --git a/internal/backend/rclone/internal_test.go b/internal/backend/rclone/internal_test.go index f6b7336fb..c43b27726 100644 --- a/internal/backend/rclone/internal_test.go +++ b/internal/backend/rclone/internal_test.go @@ -32,7 +32,7 @@ func TestRcloneExit(t *testing.T) { rtest.OK(t, err) t.Log("killed rclone") - for i := 0; i < 10; i++ { + for range 10 { _, err = be.Stat(context.TODO(), backend.Handle{ Name: "foo", Type: backend.PackFile, diff --git a/internal/backend/s3/s3_test.go b/internal/backend/s3/s3_test.go index 8935a88f8..e98fce12f 100644 --- a/internal/backend/s3/s3_test.go +++ b/internal/backend/s3/s3_test.go @@ -51,7 +51,7 @@ func runMinio(ctx context.Context, t testing.TB, dir, key, secret string) func() // wait until the TCP port is reachable var success bool - for i := 0; i < 100; i++ { + for range 100 { time.Sleep(200 * time.Millisecond) c, err := net.Dial("tcp", "localhost:9000") @@ -118,7 +118,7 @@ func newMinioTestSuite(t testing.TB) (*test.Suite[s3.Config], func()) { }, Factory: location.NewHTTPBackendFactory("s3", s3.ParseConfig, location.NoPassword, func(ctx context.Context, cfg s3.Config, rt http.RoundTripper, errorLog func(string, ...any)) (be backend.Backend, err error) { - for i := 0; i < 50; i++ { + for i := range 50 { be, err = s3.Create(ctx, cfg, rt, errorLog) if err != nil { t.Logf("s3 open: try %d: error %v", i, err) diff --git a/internal/backend/sema/backend_test.go b/internal/backend/sema/backend_test.go index 96500dd68..582a3114b 100644 --- a/internal/backend/sema/backend_test.go +++ b/internal/backend/sema/backend_test.go @@ -115,7 +115,7 @@ func concurrencyTester(t *testing.T, setup func(m *mock.Backend), handler func(b be := sema.NewBackend(m) var wg errgroup.Group - for i := 0; i < workerCount; i++ { + for range workerCount { wg.Go(handler(be)) } diff --git a/internal/backend/test/tests.go b/internal/backend/test/tests.go index 03ee84824..2c996b2ac 100644 --- a/internal/backend/test/tests.go +++ b/internal/backend/test/tests.go @@ -277,7 +277,7 @@ func (s *Suite[C]) TestList(t *testing.T) { var m sync.Mutex wg, ctx := errgroup.WithContext(context.TODO()) - for i := 0; i < numTestFiles; i++ { + for range numTestFiles { data := test.Random(random.Int(), random.Intn(100)+55) wg.Go(func() error { id := restic.Hash(data) @@ -354,7 +354,7 @@ func (s *Suite[C]) TestListCancel(t *testing.T) { testFiles := make([]backend.Handle, 0, numTestFiles) - for i := 0; i < numTestFiles; i++ { + for i := range numTestFiles { data := fmt.Appendf(nil, "random test blob %v", i) id := restic.Hash(data) h := backend.Handle{Type: backend.PackFile, Name: id.String()} @@ -768,7 +768,7 @@ func (s *Suite[C]) delayedRemove(t testing.TB, be backend.Backend, handles ...ba func delayedList(t testing.TB, b backend.Backend, tpe backend.FileType, max int, maxwait time.Duration) restic.IDs { list := restic.NewIDSet() start := time.Now() - for i := 0; i < max; i++ { + for range max { err := b.List(context.TODO(), tpe, func(fi backend.FileInfo) error { id := restic.TestParseID(fi.Name) list.Insert(id) diff --git a/internal/bloblru/cache_test.go b/internal/bloblru/cache_test.go index 5fe7bc73c..dd3e0c43d 100644 --- a/internal/bloblru/cache_test.go +++ b/internal/bloblru/cache_test.go @@ -94,7 +94,7 @@ func TestCacheGetOrCompute(t *testing.T) { calls := make(chan struct{}, 10) // start a bunch of blocking goroutines - for i := 0; i < 10; i++ { + for range 10 { wg.Go(func() error { buf, err := c.GetOrCompute(id2, func() ([]byte, error) { // block to ensure that multiple requests are waiting in parallel diff --git a/internal/checker/checker_test.go b/internal/checker/checker_test.go index d755f1201..f70f96fe7 100644 --- a/internal/checker/checker_test.go +++ b/internal/checker/checker_test.go @@ -609,7 +609,7 @@ func benchmarkSnapshotScaling(t *testing.B, newSnapshots int) { treeID := sn2.Tree - for i := 0; i < newSnapshots; i++ { + for i := range newSnapshots { sn, err := data.NewSnapshot([]string{"test" + strconv.Itoa(i)}, nil, "", time.Now()) if err != nil { t.Fatal(err) diff --git a/internal/data/find_test.go b/internal/data/find_test.go index 284566027..f1d9a7924 100644 --- a/internal/data/find_test.go +++ b/internal/data/find_test.go @@ -88,7 +88,7 @@ func TestFindUsedBlobs(t *testing.T) { repo := repository.TestRepository(t) var snapshots []*data.Snapshot - for i := 0; i < findTestSnapshots; i++ { + for i := range findTestSnapshots { sn := data.TestCreateSnapshot(t, repo, findTestTime.Add(time.Duration(i)*time.Second), findTestDepth) t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str()) snapshots = append(snapshots, sn) @@ -131,7 +131,7 @@ func TestMultiFindUsedBlobs(t *testing.T) { repo := repository.TestRepository(t) var snapshotTrees restic.IDs - for i := 0; i < findTestSnapshots; i++ { + for i := range findTestSnapshots { sn := data.TestCreateSnapshot(t, repo, findTestTime.Add(time.Duration(i)*time.Second), findTestDepth) t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str()) snapshotTrees = append(snapshotTrees, *sn.Tree) diff --git a/internal/data/testing.go b/internal/data/testing.go index 79b0ade5d..6457d97a4 100644 --- a/internal/data/testing.go +++ b/internal/data/testing.go @@ -75,7 +75,7 @@ func (fs *fakeFileSystem) saveTree(ctx context.Context, uploader restic.BlobSave numNodes := int(rnd.Int63() % maxNodes) var nodes []*Node - for i := 0; i < numNodes; i++ { + for i := range numNodes { // randomly select the type of the node, either tree (p = 1/4) or file (p = 3/4). if depth > 1 && rnd.Int63()%4 == 0 { treeSeed := rnd.Int63() % maxSeed diff --git a/internal/data/testing_test.go b/internal/data/testing_test.go index 35d32c04b..cc8bc39a7 100644 --- a/internal/data/testing_test.go +++ b/internal/data/testing_test.go @@ -20,7 +20,7 @@ const ( func TestCreateSnapshot(t *testing.T) { repo := repository.TestRepository(t) - for i := 0; i < testCreateSnapshots; i++ { + for i := range testCreateSnapshots { data.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth) } diff --git a/internal/data/tree_stream.go b/internal/data/tree_stream.go index baf7af2be..a2eb3f49b 100644 --- a/internal/data/tree_stream.go +++ b/internal/data/tree_stream.go @@ -201,7 +201,7 @@ func StreamTrees( // decoding a tree can take quite some time such that this can be both CPU- or IO-bound // one extra worker to handle huge tree blobs workerCount := int(repo.Connections()) + runtime.GOMAXPROCS(0) + 1 - for i := 0; i < workerCount; i++ { + for i := range workerCount { workerLoaderChan := loaderChan if i == 0 { workerLoaderChan = hugeTreeChan diff --git a/internal/fs/node_test.go b/internal/fs/node_test.go index 50763fc06..4f087635e 100644 --- a/internal/fs/node_test.go +++ b/internal/fs/node_test.go @@ -198,7 +198,7 @@ func TestNodeRestoreAt(t *testing.T) { // Update the tests to use UPPER case xattr names for windows. extAttrArr := test.ExtendedAttributes // Iterate through the array using pointers - for i := 0; i < len(extAttrArr); i++ { + for i := range extAttrArr { extAttrArr[i].Name = strings.ToUpper(extAttrArr[i].Name) } } diff --git a/internal/fuse/fuse_test.go b/internal/fuse/fuse_test.go index 719d6c2eb..9e9afbcaf 100644 --- a/internal/fuse/fuse_test.go +++ b/internal/fuse/fuse_test.go @@ -134,7 +134,7 @@ func TestFuseFile(t *testing.T) { rtest.Equals(t, node.Size, attr.Size) rtest.Equals(t, (node.Size/uint64(attr.BlockSize))+1, attr.Blocks) - for i := 0; i < 200; i++ { + for i := range 200 { offset := rand.Intn(int(filesize)) length := rand.Intn(int(filesize)-offset) + 100 diff --git a/internal/repository/checker.go b/internal/repository/checker.go index bb7e6ff96..5855dbffa 100644 --- a/internal/repository/checker.go +++ b/internal/repository/checker.go @@ -291,7 +291,7 @@ func (c *Checker) ReadPacks(ctx context.Context, filter func(packs map[restic.ID // as packs are streamed the concurrency is limited by IO workerCount := int(c.repo.Connections()) // run workers - for i := 0; i < workerCount; i++ { + for range workerCount { g.Go(func() error { bufRd := bufio.NewReaderSize(nil, maxStreamBufferSize) dec, err := zstd.NewReader(nil) diff --git a/internal/repository/crypto/crypto.go b/internal/repository/crypto/crypto.go index d7ac9c3d4..d8b7663f0 100644 --- a/internal/repository/crypto/crypto.go +++ b/internal/repository/crypto/crypto.go @@ -145,7 +145,7 @@ func (m *MACKey) UnmarshalJSON(data []byte) error { // Valid tests whether the key k is valid (i.e. not zero). func (m *MACKey) Valid() bool { nonzeroK := false - for i := 0; i < len(m.K); i++ { + for i := range len(m.K) { if m.K[i] != 0 { nonzeroK = true } @@ -155,7 +155,7 @@ func (m *MACKey) Valid() bool { return false } - for i := 0; i < len(m.R); i++ { + for i := range len(m.R) { if m.R[i] != 0 { return true } @@ -183,7 +183,7 @@ func (k *EncryptionKey) UnmarshalJSON(data []byte) error { // Valid tests whether the key k is valid (i.e. not zero). func (k *EncryptionKey) Valid() bool { - for i := 0; i < len(k); i++ { + for i := range len(k) { if k[i] != 0 { return true } diff --git a/internal/repository/crypto/crypto_int_test.go b/internal/repository/crypto/crypto_int_test.go index a370ef32e..966dea53b 100644 --- a/internal/repository/crypto/crypto_int_test.go +++ b/internal/repository/crypto/crypto_int_test.go @@ -171,7 +171,7 @@ func TestNonceValid(t *testing.T) { t.Error("null nonce detected as valid") } - for i := 0; i < 100; i++ { + for range 100 { nonce = NewRandomNonce() if !validNonce(nonce) { t.Errorf("random nonce not detected as valid: %02x", nonce) diff --git a/internal/repository/index/index_test.go b/internal/repository/index/index_test.go index d5a779da3..f0a395d7d 100644 --- a/internal/repository/index/index_test.go +++ b/internal/repository/index/index_test.go @@ -20,12 +20,12 @@ func TestIndexSerialize(t *testing.T) { idx := index.NewIndex() // create 50 packs with 20 blobs each - for i := 0; i < 50; i++ { + for i := range 50 { packID := restic.NewRandomID() var blobs pack.Blobs pos := uint(0) - for j := 0; j < 20; j++ { + for j := range 20 { length := uint(i*100 + j) uncompressedLength := uint(0) if i >= 25 { @@ -84,12 +84,12 @@ func TestIndexSerialize(t *testing.T) { // add more blobs to idx newtests := []*pack.PackedBlob{} - for i := 0; i < 10; i++ { + for i := range 10 { packID := restic.NewRandomID() var blobs pack.Blobs pos := uint(0) - for j := 0; j < 10; j++ { + for j := range 10 { length := uint(i*100 + j) pb := &pack.PackedBlob{ Pack: packID, @@ -144,12 +144,12 @@ func TestIndexSize(t *testing.T) { packs := 200 blobCount := 100 - for i := 0; i < packs; i++ { + for i := range packs { packID := restic.NewRandomID() var blobs pack.Blobs pos := uint(0) - for j := 0; j < blobCount; j++ { + for j := range blobCount { length := uint(i*100 + j) blobs = append(blobs, pack.Blob{ BlobHandle: restic.NewRandomBlobHandle(), @@ -400,7 +400,7 @@ func TestIndexPacks(t *testing.T) { idx := index.NewIndex() packs := restic.NewIDSet() - for i := 0; i < 20; i++ { + for range 20 { packID := restic.NewRandomID() idx.StorePack(packID, pack.Blobs{ { @@ -432,7 +432,7 @@ func createRandomIndex(rng *rand.Rand, packfiles int) (idx *index.Index, lookupB idx.Preallocate(restic.DataBlob, packfiles*9) // create index with given number of pack files - for i := 0; i < packfiles; i++ { + for i := range packfiles { packID := NewRandomTestID(rng) var blobs pack.Blobs offset := 0 @@ -523,12 +523,12 @@ func TestIndexHas(t *testing.T) { idx := index.NewIndex() // create 50 packs with 20 blobs each - for i := 0; i < 50; i++ { + for i := range 50 { packID := restic.NewRandomID() var blobs pack.Blobs pos := uint(0) - for j := 0; j < 20; j++ { + for j := range 20 { length := uint(i*100 + j) uncompressedLength := uint(0) if i >= 25 { @@ -564,7 +564,7 @@ func TestMixedEachByPack(t *testing.T) { expected := make(map[restic.ID]int) // create 50 packs with 2 blobs each - for i := 0; i < 50; i++ { + for range 50 { packID := restic.NewRandomID() expected[packID] = 1 blobs := pack.Blobs{ @@ -602,7 +602,7 @@ func TestEachByPackIgnoes(t *testing.T) { ignores := restic.NewIDSet() expected := make(map[restic.ID]int) // create 50 packs with one blob each - for i := 0; i < 50; i++ { + for i := range 50 { packID := restic.NewRandomID() if i < 3 { ignores.Insert(packID) diff --git a/internal/repository/index/indexmap_test.go b/internal/repository/index/indexmap_test.go index 085d6acb1..50c1f9557 100644 --- a/internal/repository/index/indexmap_test.go +++ b/internal/repository/index/indexmap_test.go @@ -42,7 +42,7 @@ func TestIndexMapForeach(t *testing.T) { // empty iteration } - for i := 0; i < N; i++ { + for i := range N { var id restic.ID id[0] = byte(i) m.add(id, uint32(i), uint32(i), uint32(i), uint32(i/2)) @@ -90,11 +90,11 @@ func TestIndexMapForeachWithID(t *testing.T) { rtest.Equals(t, 0, n) // Test insertion and retrieval of duplicates. - for i := 0; i < ndups; i++ { + for i := range ndups { m.add(id, uint32(i), 0, 0, 0) } - for i := 0; i < 100; i++ { + for range 100 { var otherid restic.ID r.Read(otherid[:]) m.add(otherid, math.MaxUint32, 0, 0, 0) @@ -116,13 +116,13 @@ func TestIndexMapForeachWithID(t *testing.T) { func TestHashedArrayTree(t *testing.T) { hat := newHAT() const testSize = 1024 - for i := uint(0); i < testSize; i++ { + for i := range uint(testSize) { rtest.Assert(t, hat.Size() == i, "expected hat size %v got %v", i, hat.Size()) e, idx := hat.Alloc() rtest.Assert(t, idx == i, "expected entry at idx %v got %v", i, idx) e.length = uint32(i) } - for i := uint(0); i < testSize; i++ { + for i := range uint(testSize) { e := hat.Ref(i) rtest.Assert(t, e.length == uint32(i), "expected entry to contain %v got %v", uint32(i), e.length) } diff --git a/internal/repository/index/master_index.go b/internal/repository/index/master_index.go index 500989c5a..7a03587cf 100644 --- a/internal/repository/index/master_index.go +++ b/internal/repository/index/master_index.go @@ -452,7 +452,7 @@ func (mi *MasterIndex) Rewrite(ctx context.Context, repo restic.Unpacked[restic. // the index files are probably already cached at this point loaderCount := runtime.GOMAXPROCS(0) // run workers on ch - for i := 0; i < loaderCount; i++ { + for range loaderCount { rewriteWg.Add(1) wg.Go(loader) } @@ -668,7 +668,7 @@ func (mi *MasterIndex) ListPacks(ctx context.Context, packs restic.IDSet) <-chan go func() { defer close(out) // only resort a part of the index to keep the memory overhead bounded - for i := byte(0); i < 16; i++ { + for i := range byte(16) { packBlob := make(map[restic.ID]pack.Blobs) for pack := range packs { if pack[0]&0xf == i { diff --git a/internal/repository/index/master_index_test.go b/internal/repository/index/master_index_test.go index 97fcb0ef3..36673f848 100644 --- a/internal/repository/index/master_index_test.go +++ b/internal/repository/index/master_index_test.go @@ -416,7 +416,7 @@ var ( func createFilledRepo(t testing.TB, snapshots int, version uint) (*repository.Repository, restic.Unpacked[restic.FileType]) { repo, unpacked, _ := repository.TestRepositoryWithVersion(t, version) - for i := 0; i < snapshots; i++ { + for i := range snapshots { data.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth) } return repo, unpacked @@ -595,11 +595,11 @@ func TestRewriteOversizedIndex(t *testing.T) { // build oversized index idx := index.NewIndex() numPacks := 5 - for p := 0; p < numPacks; p++ { + for range numPacks { packID := restic.NewRandomID() packBlobs := make(pack.Blobs, 0, fullIndexCount) - for i := 0; i < fullIndexCount; i++ { + for i := range fullIndexCount { blob := pack.Blob{ BlobHandle: restic.BlobHandle{ Type: restic.DataBlob, diff --git a/internal/repository/lock_file.go b/internal/repository/lock_file.go index cdc295f0d..b05bf286a 100644 --- a/internal/repository/lock_file.go +++ b/internal/repository/lock_file.go @@ -164,7 +164,7 @@ func (l *lockHandle) checkForOtherLocks(ctx context.Context) error { } delay := initialWaitBetweenLockRetries // retry locking a few times - for i := 0; i < 4; i++ { + for i := range 4 { if i != 0 { // sleep between retries to give backend some time to settle if err := cancelableDelay(ctx, delay); err != nil { diff --git a/internal/repository/pack/pack.go b/internal/repository/pack/pack.go index 43254b80b..b0a4be24c 100644 --- a/internal/repository/pack/pack.go +++ b/internal/repository/pack/pack.go @@ -143,7 +143,7 @@ func verifyHeader(k *crypto.Key, header []byte, expected []Blob) error { if len(decoded) != len(expected) { return fmt.Errorf("pack header size mismatch") } - for i := 0; i < len(decoded); i++ { + for i := range decoded { if decoded[i] != expected[i] { return fmt.Errorf("pack header entry mismatch got %v instead of %v", decoded[i], expected[i]) } diff --git a/internal/repository/pack/pack_internal_test.go b/internal/repository/pack/pack_internal_test.go index bbac5352a..0a3ed4ef2 100644 --- a/internal/repository/pack/pack_internal_test.go +++ b/internal/repository/pack/pack_internal_test.go @@ -169,8 +169,8 @@ func TestReadRecords(t *testing.T) { testReadRecords(dataSize+3, 1, 1) testReadRecords(dataSize+4, 1, 1) - for i := 0; i < 2; i++ { - for j := 0; j < 2; j++ { + for i := range 2 { + for j := range 2 { testReadRecords(dataSize, i, j) } } diff --git a/internal/repository/packer_manager_test.go b/internal/repository/packer_manager_test.go index b91d2262f..d174a1939 100644 --- a/internal/repository/packer_manager_test.go +++ b/internal/repository/packer_manager_test.go @@ -24,7 +24,7 @@ func randomID(rd io.Reader) restic.ID { const maxBlobSize = 1 << 20 func fillPacks(t testing.TB, rnd *rand.Rand, pm *packerManager, buf []byte) (bytes int) { - for i := 0; i < 102; i++ { + for range 102 { l := rnd.Intn(maxBlobSize) id := randomID(rnd) buf = buf[:l] diff --git a/internal/repository/prune_internal_test.go b/internal/repository/prune_internal_test.go index e49009813..9fec41c68 100644 --- a/internal/repository/prune_internal_test.go +++ b/internal/repository/prune_internal_test.go @@ -33,7 +33,7 @@ func TestPruneMaxUnusedDuplicate(t *testing.T) { const blobSize = 1024 * 1024 bufs := [][]byte{} - for i := 0; i < 4; i++ { + for range 4 { // use uniform length for simpler control via MaxUnusedBytes buf := make([]byte, blobSize) random.Read(buf) diff --git a/internal/repository/prune_test.go b/internal/repository/prune_test.go index cf5664104..f514ddd7c 100644 --- a/internal/repository/prune_test.go +++ b/internal/repository/prune_test.go @@ -134,7 +134,7 @@ func TestPruneSmall(t *testing.T) { keep := restic.NewBlobSet() rtest.OK(t, repo.WithBlobUploader(context.TODO(), func(ctx context.Context, uploader restic.BlobSaverWithAsync) error { // we need a minimum of 11 packfiles, each packfile will be about 5 Mb long - for i := 0; i < numBlobsCreated; i++ { + for range numBlobsCreated { buf := make([]byte, blobSize) random.Read(buf) diff --git a/internal/repository/raw_test.go b/internal/repository/raw_test.go index ff57a60ed..30df82d0f 100644 --- a/internal/repository/raw_test.go +++ b/internal/repository/raw_test.go @@ -24,7 +24,7 @@ func TestLoadRaw(t *testing.T) { repo, err := repository.New(b, repository.Options{}) rtest.OK(t, err) - for i := 0; i < 5; i++ { + for i := range 5 { data := rtest.Random(23+i, 500*KiB) id := restic.Hash(data) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 8955164ae..46bd4cf55 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -806,7 +806,7 @@ func (r *Repository) createIndexFromPacks(ctx context.Context, packsize map[rest // decoding the pack header is usually quite fast, thus we are primarily IO-bound workerCount := int(r.Connections()) // run workers on ch - for i := 0; i < workerCount; i++ { + for range workerCount { wg.Go(worker) } @@ -1110,7 +1110,7 @@ func streamPack(ctx context.Context, beLoad backendLoadFn, loadBlobFn loadBlobFn lastPos := blobs[0].Offset const maxChunkSize = 2 * DefaultPackSize - for i := 0; i < len(blobs); i++ { + for i := range blobs { if blobs[i].Offset < lastPos { // don't wait for streamPackPart to fail return errors.Errorf("overlapping blobs in pack %v", packID) diff --git a/internal/repository/repository_internal_test.go b/internal/repository/repository_internal_test.go index 1b78f07cb..e5e0c4b5a 100644 --- a/internal/repository/repository_internal_test.go +++ b/internal/repository/repository_internal_test.go @@ -34,7 +34,7 @@ func TestSortCachedPacksFirst(t *testing.T) { r = rand.New(rand.NewSource(1261)) ) - for i := 0; i < len(blobs); i++ { + for i := range len(blobs) { var id restic.ID r.Read(id[:]) blobs[i] = &pack.PackedBlob{Pack: id, Blob: pack.Blob{}} @@ -65,7 +65,7 @@ func BenchmarkSortCachedPacksFirst(b *testing.B) { r = rand.New(rand.NewSource(1261)) ) - for i := 0; i < nblobs; i++ { + for i := range nblobs { var id restic.ID r.Read(id[:]) blobs[i] = &pack.PackedBlob{Pack: id, Blob: pack.Blob{}} @@ -96,7 +96,7 @@ func benchmarkLoadIndex(b *testing.B, version uint) { repo, _, be := TestRepositoryWithVersion(b, version) idx := index.NewIndex() - for i := 0; i < 5000; i++ { + for range 5000 { idx.StorePack(restic.NewRandomID(), pack.Blobs{ { BlobHandle: restic.NewRandomBlobHandle(), diff --git a/internal/repository/repository_test.go b/internal/repository/repository_test.go index cc0274066..5e7a88948 100644 --- a/internal/repository/repository_test.go +++ b/internal/repository/repository_test.go @@ -391,7 +391,7 @@ func TestRepositoryLoadUnpackedRetryBroken(t *testing.T) { func saveRandomDataBlobs(t testing.TB, repo restic.Repository, num int, sizeMax int) { rnd := rand.New(rand.NewSource(time.Now().UnixNano())) rtest.OK(t, repo.WithBlobUploader(context.TODO(), func(ctx context.Context, uploader restic.BlobSaverWithAsync) error { - for i := 0; i < num; i++ { + for range num { size := rnd.Int() % sizeMax buf := make([]byte, size) @@ -413,7 +413,7 @@ func testRepositoryIncrementalIndex(t *testing.T, version uint) { repo, _, _ := repository.TestRepositoryWithVersion(t, version) // add a few rounds of packs - for j := 0; j < 5; j++ { + for range 5 { // add some packs and write index saveRandomDataBlobs(t, repo, 20, 1<<15) } @@ -532,7 +532,7 @@ func TestSaveBlobAsync(t *testing.T) { err := repo.WithBlobUploader(ctx, func(ctx context.Context, uploader restic.BlobSaverWithAsync) error { var wg sync.WaitGroup wg.Add(numCalls) - for i := 0; i < numCalls; i++ { + for i := range numCalls { // Use unique data for each call testData := fmt.Appendf(nil, "test blob data %d", i) uploader.SaveBlobAsync(ctx, restic.DataBlob, testData, restic.ID{}, false, diff --git a/internal/restic/blob_set_test.go b/internal/restic/blob_set_test.go index 4e0961aa5..4786dd2d4 100644 --- a/internal/restic/blob_set_test.go +++ b/internal/restic/blob_set_test.go @@ -21,7 +21,7 @@ func TestBlobSetString(t *testing.T) { rtest.Equals(t, "{}", s.String()) var h BlobHandle - for i := 0; i < 100; i++ { + for range 100 { h.Type = DataBlob _, _ = random.Read(h.ID[:]) s.Insert(h) diff --git a/internal/restic/parallel.go b/internal/restic/parallel.go index f7fffa22e..ffc5c4861 100644 --- a/internal/restic/parallel.go +++ b/internal/restic/parallel.go @@ -44,7 +44,7 @@ func ParallelList(ctx context.Context, r Lister, t FileType, parallelism uint, f } // run workers on ch - for i := uint(0); i < parallelism; i++ { + for range parallelism { wg.Go(worker) } diff --git a/internal/restic/zeroprefix_test.go b/internal/restic/zeroprefix_test.go index a21806851..870d9f6d2 100644 --- a/internal/restic/zeroprefix_test.go +++ b/internal/restic/zeroprefix_test.go @@ -18,7 +18,7 @@ func TestZeroPrefixLen(t *testing.T) { test.Equals(t, i, skipped) } // test buffers of various sizes - for i := 0; i < len(buf); i++ { + for i := range len(buf) { skipped := restic.ZeroPrefixLen(buf[i:]) test.Equals(t, 0, skipped) } diff --git a/internal/restorer/filerestorer_test.go b/internal/restorer/filerestorer_test.go index 8526fc608..2b03b70d4 100644 --- a/internal/restorer/filerestorer_test.go +++ b/internal/restorer/filerestorer_test.go @@ -328,7 +328,7 @@ func TestFileRestorerFrequentBlob(t *testing.T) { blobs := []TestBlob{ {"data1-1", "pack1-1"}, } - for i := 0; i < 10000; i++ { + for range 10000 { blobs = append(blobs, TestBlob{"a", "pack1-1"}) } blobs = append(blobs, TestBlob{"end", "pack1-1"}) diff --git a/internal/restorer/fileswriter.go b/internal/restorer/fileswriter.go index 20d458343..ae49e36cc 100644 --- a/internal/restorer/fileswriter.go +++ b/internal/restorer/fileswriter.go @@ -41,7 +41,7 @@ func newFilesWriter(count int, allowRecursiveDelete bool) *filesWriter { // use a large number of buckets to minimize bucket contention // creating a new file can be slow, so make sure that files typically end up in different buckets. buckets := make([]filesWriterBucket, 1024) - for b := 0; b < len(buckets); b++ { + for b := range buckets { buckets[b].files = make(map[string]*partialFile) } diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index 98b28f915..cc7e1df9a 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -660,7 +660,7 @@ func (res *Restorer) VerifyFiles(ctx context.Context, dst string, countRestoredF return err }) - for i := 0; i < nVerifyWorkers; i++ { + for range nVerifyWorkers { g.Go(func() (err error) { var buf []byte for job := range work { diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index 420547192..29b1d770a 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -1108,7 +1108,7 @@ func TestRestorerOverwriteBehavior(t *testing.T) { func TestRestorerOverwritePartial(t *testing.T) { parts := make([]string, 100) size := 0 - for i := 0; i < len(parts); i++ { + for i := range parts { parts[i] = fmt.Sprint(i) size += len(parts[i]) if i < 8 { @@ -1503,7 +1503,7 @@ func TestRestorerLongPath(t *testing.T) { tmp := t.TempDir() longPath := tmp - for i := 0; i < 20; i++ { + for range 20 { longPath = filepath.Join(longPath, "aaaaaaaaaaaaaaaaaaaa") } diff --git a/internal/selfupdate/download_test.go b/internal/selfupdate/download_test.go index f4788072d..acbae0465 100644 --- a/internal/selfupdate/download_test.go +++ b/internal/selfupdate/download_test.go @@ -30,7 +30,7 @@ func TestExtractToFileZip(t *testing.T) { rtest.OK(t, zw.Close()) // run twice to test creating a new file and overwriting - for i := 0; i < 2; i++ { + for range 2 { outfn := filepath.Join(dir, ext+"-out") rtest.OK(t, extractToFile(archive.Bytes(), "src."+ext, outfn, printf)) diff --git a/internal/ui/progress/counter_test.go b/internal/ui/progress/counter_test.go index 4c591e534..5db1eaa9e 100644 --- a/internal/ui/progress/counter_test.go +++ b/internal/ui/progress/counter_test.go @@ -40,7 +40,7 @@ func TestCounter(t *testing.T) { done := make(chan struct{}) go func() { defer close(done) - for i := 0; i < N; i++ { + for range N { time.Sleep(time.Millisecond) c.Add(1) }