From 62ed24a87c337e91ea14990d526dd6b00cf0add2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 15 Dec 2025 18:27:58 +0100 Subject: [PATCH] modernize: Use slices.Contains MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- .../internal/tarsum/versioning_test.go | 8 ++------ daemon/daemon_linux.go | 7 +++---- daemon/internal/distribution/pull_v2.go | 7 +++---- daemon/libnetwork/controller.go | 7 +++---- daemon/libnetwork/iptables/firewalld.go | 14 ++++---------- daemon/libnetwork/network.go | 6 ++---- daemon/libnetwork/networkdb/cluster.go | 8 +++----- daemon/libnetwork/networkdb/delegate.go | 8 +++----- daemon/libnetwork/networkdb/networkdb.go | 7 +++---- daemon/list_test.go | 7 +++---- daemon/network.go | 7 +++---- daemon/oci_linux.go | 8 ++------ daemon/pkg/oci/caps/utils.go | 8 ++------ daemon/pkg/opts/opts.go | 8 ++------ daemon/pkg/plugin/v2/settable.go | 15 ++++----------- daemon/volume/service/store.go | 8 ++------ integration-cli/docker_cli_build_test.go | 16 ++++------------ pkg/plugins/plugins.go | 8 ++------ 18 files changed, 50 insertions(+), 107 deletions(-) diff --git a/daemon/builder/remotecontext/internal/tarsum/versioning_test.go b/daemon/builder/remotecontext/internal/tarsum/versioning_test.go index da7946f636..285d4f9480 100644 --- a/daemon/builder/remotecontext/internal/tarsum/versioning_test.go +++ b/daemon/builder/remotecontext/internal/tarsum/versioning_test.go @@ -4,6 +4,7 @@ import ( "archive/tar" "errors" "fmt" + "slices" "strings" "testing" @@ -96,12 +97,7 @@ func TestGetVersions(t *testing.T) { } func containsVersion(versions []Version, version Version) bool { - for _, v := range versions { - if v == version { - return true - } - } - return false + return slices.Contains(versions, version) } func TestSelectXattrsV1(t *testing.T) { diff --git a/daemon/daemon_linux.go b/daemon/daemon_linux.go index 5adbbad299..37259d65f8 100644 --- a/daemon/daemon_linux.go +++ b/daemon/daemon_linux.go @@ -7,6 +7,7 @@ import ( "io" "os" "regexp" + "slices" "strings" "sync" @@ -231,10 +232,8 @@ func supportsRecursivelyReadOnly(cfg *configStore, runtime string) error { if features == nil { return fmt.Errorf("rro is not supported by runtime %q: OCI features struct is not available", runtime) } - for _, s := range features.MountOptions { - if s == "rro" { - return nil - } + if slices.Contains(features.MountOptions, "rro") { + return nil } return fmt.Errorf("rro is not supported by runtime %q", runtime) } diff --git a/daemon/internal/distribution/pull_v2.go b/daemon/internal/distribution/pull_v2.go index 8a2f26899e..4156183d62 100644 --- a/daemon/internal/distribution/pull_v2.go +++ b/daemon/internal/distribution/pull_v2.go @@ -6,6 +6,7 @@ import ( "io" "os" "runtime" + "slices" "strings" "time" @@ -452,10 +453,8 @@ func (p *puller) validateMediaType(mediaType string) error { } else { allowedMediaTypes = defaultImageTypes } - for _, t := range allowedMediaTypes { - if mediaType == t { - return nil - } + if slices.Contains(allowedMediaTypes, mediaType) { + return nil } configClass := mediaTypeClasses[mediaType] diff --git a/daemon/libnetwork/controller.go b/daemon/libnetwork/controller.go index 637e46d4af..64d6d9f7fd 100644 --- a/daemon/libnetwork/controller.go +++ b/daemon/libnetwork/controller.go @@ -49,6 +49,7 @@ import ( "net" "path/filepath" "runtime" + "slices" "strings" "sync" "time" @@ -831,10 +832,8 @@ func (c *Controller) Networks(ctx context.Context) []*Network { // WalkNetworks uses the provided function to walk the Network(s) managed by this controller. func (c *Controller) WalkNetworks(walker NetworkWalker) { - for _, n := range c.Networks(context.TODO()) { - if walker(n) { - return - } + if slices.ContainsFunc(c.Networks(context.TODO()), walker) { + return } } diff --git a/daemon/libnetwork/iptables/firewalld.go b/daemon/libnetwork/iptables/firewalld.go index 5453d6c520..7c82e9dab4 100644 --- a/daemon/libnetwork/iptables/firewalld.go +++ b/daemon/libnetwork/iptables/firewalld.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "os" + "slices" "strings" "sync" "sync/atomic" @@ -177,10 +178,8 @@ func reloaded() { // OnReloaded add callback func OnReloaded(callback func()) { - for _, pf := range onReloaded { - if pf == &callback { - return - } + if slices.Contains(onReloaded, &callback) { + return } onReloaded = append(onReloaded, &callback) } @@ -366,10 +365,5 @@ type interfaceNotFound struct{ error } func (interfaceNotFound) NotFound() {} func contains(list []string, val string) bool { - for _, v := range list { - if v == val { - return true - } - } - return false + return slices.Contains(list, val) } diff --git a/daemon/libnetwork/network.go b/daemon/libnetwork/network.go index be7d82d6dc..ad87d74c03 100644 --- a/daemon/libnetwork/network.go +++ b/daemon/libnetwork/network.go @@ -1275,10 +1275,8 @@ func (n *Network) HasContainerAttachments() bool { // WalkEndpoints uses the provided function to walk the Endpoints. func (n *Network) WalkEndpoints(walker EndpointWalker) { - for _, e := range n.Endpoints() { - if walker(e) { - return - } + if slices.ContainsFunc(n.Endpoints(), walker) { + return } } diff --git a/daemon/libnetwork/networkdb/cluster.go b/daemon/libnetwork/networkdb/cluster.go index c356abf8d1..2ed3a2c619 100644 --- a/daemon/libnetwork/networkdb/cluster.go +++ b/daemon/libnetwork/networkdb/cluster.go @@ -9,6 +9,7 @@ import ( rnd "math/rand" "net" "net/netip" + "slices" "strings" "time" @@ -543,11 +544,8 @@ func (nDB *NetworkDB) bulkSyncTables() { updatedNetworks := make([]string, 0, len(networks)) for _, nid := range networks { var found bool - for _, completedNid := range completed { - if nid == completedNid { - found = true - break - } + if slices.Contains(completed, nid) { + found = true } if !found { diff --git a/daemon/libnetwork/networkdb/delegate.go b/daemon/libnetwork/networkdb/delegate.go index 198dc7bea6..85bdc37af5 100644 --- a/daemon/libnetwork/networkdb/delegate.go +++ b/daemon/libnetwork/networkdb/delegate.go @@ -3,6 +3,7 @@ package networkdb import ( "context" "net" + "slices" "time" "github.com/containerd/log" @@ -154,11 +155,8 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent, isBulkSync bool) bool // Check if the owner of the event is still part of the network nodes := nDB.networkNodes[tEvent.NetworkID] var nodePresent bool - for _, node := range nodes { - if node == tEvent.NodeName { - nodePresent = true - break - } + if slices.Contains(nodes, tEvent.NodeName) { + nodePresent = true } if !ok || network.leaving || !nodePresent { diff --git a/daemon/libnetwork/networkdb/networkdb.go b/daemon/libnetwork/networkdb/networkdb.go index 36b71d90c8..6b97bff2ff 100644 --- a/daemon/libnetwork/networkdb/networkdb.go +++ b/daemon/libnetwork/networkdb/networkdb.go @@ -9,6 +9,7 @@ import ( "math/rand/v2" "net/netip" "os" + "slices" "strings" "sync" "sync/atomic" @@ -730,10 +731,8 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error { // should hold the NetworkDB lock while calling this func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) { nodes := nDB.networkNodes[nid] - for _, node := range nodes { - if node == nodeName { - return - } + if slices.Contains(nodes, nodeName) { + return } nDB.networkNodes[nid] = append(nDB.networkNodes[nid], nodeName) diff --git a/daemon/list_test.go b/daemon/list_test.go index e389106577..f0d5c252ab 100644 --- a/daemon/list_test.go +++ b/daemon/list_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "testing" "time" @@ -72,10 +73,8 @@ func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *containe func containerListContainsName(containers []containertypes.Summary, name string) bool { for _, ctr := range containers { - for _, containerName := range ctr.Names { - if containerName == name { - return true - } + if slices.Contains(ctr.Names, name) { + return true } } diff --git a/daemon/network.go b/daemon/network.go index 859d9af722..f42d6aa806 100644 --- a/daemon/network.go +++ b/daemon/network.go @@ -7,6 +7,7 @@ import ( "maps" "net" "net/netip" + "slices" "sort" "strconv" "strings" @@ -433,10 +434,8 @@ func (daemon *Daemon) pluginRefCount(driver, capability string, mode int) { // other capabilities can be ignored for now } - for _, d := range builtinDrivers { - if d == driver { - return - } + if slices.Contains(builtinDrivers, driver) { + return } if daemon.PluginStore != nil { diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go index 237049ea20..445fd6a4da 100644 --- a/daemon/oci_linux.go +++ b/daemon/oci_linux.go @@ -6,6 +6,7 @@ import ( "maps" "os" "path/filepath" + "slices" "strconv" "strings" @@ -468,12 +469,7 @@ var ( // inSlice tests whether a string is contained in a slice of strings or not. // Comparison is case sensitive func inSlice(slice []string, s string) bool { - for _, ss := range slice { - if s == ss { - return true - } - } - return false + return slices.Contains(slice, s) } // withMounts sets the container's mounts diff --git a/daemon/pkg/oci/caps/utils.go b/daemon/pkg/oci/caps/utils.go index dc29fc429b..9bed97b297 100644 --- a/daemon/pkg/oci/caps/utils.go +++ b/daemon/pkg/oci/caps/utils.go @@ -2,6 +2,7 @@ package caps import ( "fmt" + "slices" "strings" "github.com/moby/moby/v2/errdefs" @@ -39,12 +40,7 @@ func knownCapabilities() map[string]*struct{} { // inSlice tests whether a string is contained in a slice of strings or not. func inSlice(slice []string, s string) bool { - for _, ss := range slice { - if s == ss { - return true - } - } - return false + return slices.Contains(slice, s) } const allCapabilities = "ALL" diff --git a/daemon/pkg/opts/opts.go b/daemon/pkg/opts/opts.go index 1ec148a13e..8268a6735e 100644 --- a/daemon/pkg/opts/opts.go +++ b/daemon/pkg/opts/opts.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "path" + "slices" "strings" "github.com/docker/go-units" @@ -94,12 +95,7 @@ func (opts *ListOpts) GetAllOrEmpty() []string { // Get checks the existence of the specified key. func (opts *ListOpts) Get(key string) bool { - for _, k := range *opts.values { - if k == key { - return true - } - } - return false + return slices.Contains(*opts.values, key) } // Len returns the amount of element in the slice. diff --git a/daemon/pkg/plugin/v2/settable.go b/daemon/pkg/plugin/v2/settable.go index 8d1d8ab460..f7695108af 100644 --- a/daemon/pkg/plugin/v2/settable.go +++ b/daemon/pkg/plugin/v2/settable.go @@ -3,6 +3,7 @@ package v2 import ( "errors" "fmt" + "slices" "strings" ) @@ -71,19 +72,11 @@ func (set *settable) isSettable(allowedSettableFields []string, settable []strin } } - isAllowed := false - for _, allowedSettableField := range allowedSettableFields { - if set.field == allowedSettableField { - isAllowed = true - break - } - } + isAllowed := slices.Contains(allowedSettableFields, set.field) if isAllowed { - for _, settableField := range settable { - if set.field == settableField { - return true, nil - } + if slices.Contains(settable, set.field) { + return true, nil } } diff --git a/daemon/volume/service/store.go b/daemon/volume/service/store.go index 8f6fb8a7be..7f6073a4fc 100644 --- a/daemon/volume/service/store.go +++ b/daemon/volume/service/store.go @@ -7,6 +7,7 @@ import ( "net" "os" "path/filepath" + "slices" "sync" "time" @@ -229,12 +230,7 @@ type VolumeStore struct { func filterByDriver(names []string) filterFunc { return func(v volume.Volume) bool { - for _, name := range names { - if name == v.DriverName() { - return true - } - } - return false + return slices.Contains(names, v.DriverName()) } } diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index c653c86c9c..343ddc9900 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -12,6 +12,7 @@ import ( "reflect" "regexp" "runtime" + "slices" "strconv" "strings" "testing" @@ -4357,13 +4358,7 @@ func (s *DockerCLIBuildSuite) TestBuildBuildTimeArgExpansion(c *testing.T) { var resArr []string inspectFieldAndUnmarshall(c, imgName, "Config.Env", &resArr) - found := false - for _, v := range resArr { - if fmt.Sprintf("%s=%s", envVar, envVal) == v { - found = true - break - } - } + found := slices.Contains(resArr, fmt.Sprintf("%s=%s", envVar, envVal)) if !found { c.Fatalf("Config.Env value mismatch. Expected to exist: %s=%s, got: %v", envVar, envVal, resArr) @@ -4707,11 +4702,8 @@ func (s *DockerCLIBuildSuite) TestBuildTagEvent(c *testing.T) { events := strings.Split(strings.TrimSpace(out), "\n") actions := eventActionsByIDAndType(c, events, imgName+":latest", "image") var foundTag bool - for _, a := range actions { - if a == "tag" { - foundTag = true - break - } + if slices.Contains(actions, "tag") { + foundTag = true } assert.Assert(c, foundTag, "No tag event found:\n%s", out) diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 1baa1a3465..8e2f76dc69 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -26,6 +26,7 @@ import ( "context" "errors" "fmt" + "slices" "sync" "time" @@ -193,12 +194,7 @@ func (p *Plugin) implements(kind string) bool { if p.Manifest == nil { return false } - for _, driver := range p.Manifest.Implements { - if driver == kind { - return true - } - } - return false + return slices.Contains(p.Manifest.Implements, kind) } func loadWithRetry(name string, retry bool) (*Plugin, error) {