Skip to content

Commit 6d4b3b5

Browse files
committed
modernize: slicescontains
go install golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest modernize -slicescontains -fix ./... Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 835d510 commit 6d4b3b5

7 files changed

Lines changed: 31 additions & 37 deletions

File tree

cli/command/manifest/annotate.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package manifest
25

36
import (
47
"context"
58
"fmt"
69
"path/filepath"
10+
"slices"
711

812
"github.com/containerd/errdefs"
913
"github.com/docker/cli/cli"
@@ -164,10 +168,8 @@ func runManifestAnnotate(dockerCLI command.Cli, opts annotateOptions) error {
164168
}
165169

166170
func appendIfUnique(list []string, str string) []string {
167-
for _, s := range list {
168-
if s == str {
169-
return list
170-
}
171+
if slices.Contains(list, str) {
172+
return list
171173
}
172174
return append(list, str)
173175
}

cli/command/service/update.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,7 @@ func updateStringToSlice(flags *pflag.FlagSet, flag string, field *[]string) {
552552
}
553553

554554
func anyChanged(flags *pflag.FlagSet, fields ...string) bool {
555-
for _, flag := range fields {
556-
if flags.Changed(flag) {
557-
return true
558-
}
559-
}
560-
return false
555+
return slices.ContainsFunc(fields, flags.Changed)
561556
}
562557

563558
func addGenericResources(flags *pflag.FlagSet, spec *swarm.TaskSpec) error {

cli/connhelper/connhelper.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
// Package connhelper provides helpers for connecting to a remote daemon host with custom logic.
25
package connhelper
36

@@ -6,6 +9,7 @@ import (
69
"fmt"
710
"net"
811
"net/url"
12+
"slices"
913
"strings"
1014

1115
"github.com/docker/cli/cli/connhelper/commandconn"
@@ -89,10 +93,8 @@ func addSSHTimeout(sshFlags []string) []string {
8993
// disablePseudoTerminalAllocation disables pseudo-terminal allocation to
9094
// prevent SSH from executing as a login shell
9195
func disablePseudoTerminalAllocation(sshFlags []string) []string {
92-
for _, flag := range sshFlags {
93-
if flag == "-T" {
94-
return sshFlags
95-
}
96+
if slices.Contains(sshFlags, "-T") {
97+
return sshFlags
9698
}
9799
return append(sshFlags, "-T")
98100
}

cmd/docker/docker.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package main
25

36
import (
@@ -7,6 +10,7 @@ import (
710
"os"
811
"os/exec"
912
"os/signal"
13+
"slices"
1014
"strings"
1115
"syscall"
1216

@@ -617,10 +621,8 @@ func findCommand(cmd *cobra.Command, cmds []string) bool {
617621
if cmd == nil {
618622
return false
619623
}
620-
for _, c := range cmds {
621-
if c == cmd.Name() {
622-
return true
623-
}
624+
if slices.Contains(cmds, cmd.Name()) {
625+
return true
624626
}
625627
return findCommand(cmd.Parent(), cmds)
626628
}

internal/volumespec/volumespec.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package volumespec
66
import (
77
"errors"
88
"fmt"
9+
"slices"
910
"strings"
1011
"unicode"
1112
"unicode/utf8"
@@ -89,12 +90,7 @@ func populateFieldFromBuffer(char rune, buffer []rune, volume *VolumeConfig) err
8990
}
9091

9192
func isBindOption(option string) bool {
92-
for _, propagation := range mount.Propagations {
93-
if mount.Propagation(option) == propagation {
94-
return true
95-
}
96-
}
97-
return false
93+
return slices.Contains(mount.Propagations, mount.Propagation(option))
9894
}
9995

10096
func populateType(volume *VolumeConfig) {

opts/opts.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package opts
25

36
import (
@@ -7,6 +10,7 @@ import (
710
"math/big"
811
"net"
912
"path"
13+
"slices"
1014
"strings"
1115

1216
"github.com/docker/cli/internal/lazyregexp"
@@ -104,12 +108,7 @@ func (opts *ListOpts) GetAllOrEmpty() []string {
104108

105109
// Get checks the existence of the specified key.
106110
func (opts *ListOpts) Get(key string) bool {
107-
for _, k := range *opts.values {
108-
if k == key {
109-
return true
110-
}
111-
}
112-
return false
111+
return slices.Contains(*opts.values, key)
113112
}
114113

115114
// Len returns the amount of element in the slice.

opts/swarmopts/port_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+
//go:build go1.24
3+
14
package swarmopts
25

36
import (
47
"bytes"
58
"os"
9+
"slices"
610
"testing"
711

812
"github.com/docker/go-connections/nat"
@@ -371,13 +375,7 @@ func TestConvertPortToPortConfigWithIP(t *testing.T) {
371375

372376
func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) {
373377
t.Helper()
374-
contains := false
375-
for _, portConfig := range portConfigs {
376-
if portConfig == expected {
377-
contains = true
378-
break
379-
}
380-
}
378+
contains := slices.Contains(portConfigs, expected)
381379
if !contains {
382380
t.Errorf("expected %v to contain %v, did not", portConfigs, expected)
383381
}

0 commit comments

Comments
 (0)