From a2cb0155912e05aabe4bdaf94cdbfd9a5666de2e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Wed, 22 Jul 2026 22:26:33 +0200 Subject: [PATCH] Apply go fix -slicescontains ./... --- cmd/restic/cmd_backup.go | 7 +++---- cmd/restic/cmd_backup_integration_test.go | 9 ++------- cmd/restic/cmd_cat.go | 9 ++------- cmd/restic/integration_helpers_test.go | 9 ++------- internal/data/snapshot.go | 16 +++------------- 5 files changed, 12 insertions(+), 38 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index fb81aa22e..63814fb93 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" "runtime" + "slices" "strconv" "strings" "time" @@ -304,10 +305,8 @@ func (opts BackupOptions) Check(gopts global.Options, args []string) error { } filesFrom := append(append(opts.FilesFrom, opts.FilesFromVerbatim...), opts.FilesFromRaw...) - for _, filename := range filesFrom { - if filename == "-" { - return errors.Fatal("unable to read password from stdin when data is to be read from stdin, use --password-file or $RESTIC_PASSWORD") - } + if slices.Contains(filesFrom, "-") { + return errors.Fatal("unable to read password from stdin when data is to be read from stdin, use --password-file or $RESTIC_PASSWORD") } } diff --git a/cmd/restic/cmd_backup_integration_test.go b/cmd/restic/cmd_backup_integration_test.go index e367a1761..ffb7f675f 100644 --- a/cmd/restic/cmd_backup_integration_test.go +++ b/cmd/restic/cmd_backup_integration_test.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "testing" "time" @@ -630,13 +631,7 @@ func linkEqual(source, dest []string) bool { } for i := range source { - found := false - for j := range dest { - if source[i] == dest[j] { - found = true - break - } - } + found := slices.Contains(dest, source[i]) if !found { return false } diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index 71b9c2e71..f8a33cabd 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "slices" "strings" "github.com/spf13/cobra" @@ -49,13 +50,7 @@ func validateCatArgs(args []string) error { return errors.Fatal("type not specified") } - validType := false - for _, v := range catAllowedCmds { - if v == args[0] { - validType = true - break - } - } + validType := slices.Contains(catAllowedCmds, args[0]) if !validType { return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(catAllowedCmds, "|")) } diff --git a/cmd/restic/integration_helpers_test.go b/cmd/restic/integration_helpers_test.go index b20f63208..e05c88976 100644 --- a/cmd/restic/integration_helpers_test.go +++ b/cmd/restic/integration_helpers_test.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "testing" @@ -339,13 +340,7 @@ func removePacksExcept(gopts global.Options, t testing.TB, keep restic.IDSet, re } func includes(haystack []string, needle string) bool { - for _, s := range haystack { - if s == needle { - return true - } - } - - return false + return slices.Contains(haystack, needle) } func loadSnapshotMap(t testing.TB, gopts global.Options) map[string]struct{} { diff --git a/internal/data/snapshot.go b/internal/data/snapshot.go index 6258e34b7..4e2f81503 100644 --- a/internal/data/snapshot.go +++ b/internal/data/snapshot.go @@ -5,6 +5,7 @@ import ( "fmt" "os/user" "path/filepath" + "slices" "sync" "time" @@ -174,12 +175,7 @@ func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) { } func (sn *Snapshot) hasTag(tag string) bool { - for _, snTag := range sn.Tags { - if tag == snTag { - return true - } - } - return false + return slices.Contains(sn.Tags, tag) } // HasTags returns true if the snapshot has all the tags in l. @@ -240,13 +236,7 @@ func (sn *Snapshot) HasHostname(hostnames []string) bool { return true } - for _, hostname := range hostnames { - if sn.Hostname == hostname { - return true - } - } - - return false + return slices.Contains(hostnames, sn.Hostname) } // Snapshots is a list of snapshots.