modernize: Use slices.Contains

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-12-15 18:27:58 +01:00
parent bce14ac5bc
commit 62ed24a87c
18 changed files with 50 additions and 107 deletions

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -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]

View File

@@ -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
}
}

View File

@@ -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)
}

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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"

View File

@@ -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.

View File

@@ -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
}
}

View File

@@ -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())
}
}

View File

@@ -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 <key=value> 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)

View File

@@ -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) {