Skip to content

Commit ab7e0ce

Browse files
committed
Use slices.Contains more
Replace 'for i, elem := range s { if elem == needle { ...; break }' by a call to slices.Contains, added in go1.21. Brought to you by modernize -fix -test -category slicescontains ./... Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 1c15312 commit ab7e0ce

File tree

14 files changed

+37
-97
lines changed

14 files changed

+37
-97
lines changed

cmd/podman/root.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"runtime"
1010
"runtime/pprof"
11+
"slices"
1112
"strconv"
1213
"strings"
1314

@@ -452,11 +453,8 @@ func loggingHook() {
452453
}
453454
logLevel = "debug"
454455
}
455-
for _, l := range common.LogLevels {
456-
if l == strings.ToLower(logLevel) {
457-
found = true
458-
break
459-
}
456+
if slices.Contains(common.LogLevels, strings.ToLower(logLevel)) {
457+
found = true
460458
}
461459
if !found {
462460
fmt.Fprintf(os.Stderr, "Log Level %q is not supported, choose from: %s\n", logLevel, strings.Join(common.LogLevels, ", "))

cmd/podman/validate/choice.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validate
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67
)
78

@@ -29,11 +30,9 @@ func (c *ChoiceValue) String() string {
2930
}
3031

3132
func (c *ChoiceValue) Set(value string) error {
32-
for _, v := range c.choices {
33-
if v == value {
34-
*c.value = value
35-
return nil
36-
}
33+
if slices.Contains(c.choices, value) {
34+
*c.value = value
35+
return nil
3736
}
3837
return fmt.Errorf("%q is not a valid value. Choose from: %q", value, c.Choices())
3938
}

libpod/container_commit.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"errors"
88
"fmt"
9+
"slices"
910
"strings"
1011

1112
"github.com/containers/buildah"
@@ -130,13 +131,7 @@ func (c *Container) Commit(ctx context.Context, destImage string, options Contai
130131
// Only include anonymous named volumes added by the user by
131132
// default.
132133
for _, v := range c.config.NamedVolumes {
133-
include := false
134-
for _, userVol := range c.config.UserVolumes {
135-
if userVol == v.Dest {
136-
include = true
137-
break
138-
}
139-
}
134+
include := slices.Contains(c.config.UserVolumes, v.Dest)
140135
if include {
141136
vol, err := c.runtime.GetVolume(v.Name)
142137
if err != nil {

libpod/container_internal.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,7 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (_ bool, retErr err
351351
// Returns true if the container is in one of the given states,
352352
// or false otherwise.
353353
func (c *Container) ensureState(states ...define.ContainerStatus) bool {
354-
for _, state := range states {
355-
if state == c.state.State {
356-
return true
357-
}
358-
}
359-
return false
354+
return slices.Contains(states, c.state.State)
360355
}
361356

362357
// Sync this container with on-disk state and runtime status
@@ -484,12 +479,7 @@ func (c *Container) setupStorage(ctx context.Context) error {
484479
}
485480
if c.config.Privileged {
486481
privOpt := func(opt string) bool {
487-
for _, privopt := range []string{"nodev", "nosuid", "noexec"} {
488-
if opt == privopt {
489-
return true
490-
}
491-
}
492-
return false
482+
return slices.Contains([]string{"nodev", "nosuid", "noexec"}, opt)
493483
}
494484

495485
defOptions, err := storage.GetMountOptions(c.runtime.store.GraphDriverName(), c.runtime.store.GraphOptions())

libpod/container_internal_common.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,11 +2730,8 @@ func (c *Container) userPasswdEntry(u *user.User) (string, error) {
27302730
hDir = filepath.Dir(hDir)
27312731
}
27322732
if homeDir != u.HomeDir {
2733-
for _, hDir := range c.UserVolumes() {
2734-
if hDir == u.HomeDir {
2735-
homeDir = u.HomeDir
2736-
break
2737-
}
2733+
if slices.Contains(c.UserVolumes(), u.HomeDir) {
2734+
homeDir = u.HomeDir
27382735
}
27392736
}
27402737

libpod/options.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"os"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314
"syscall"
1415
"time"
@@ -274,10 +275,8 @@ func WithHooksDir(hooksDirs ...string) RuntimeOption {
274275
return define.ErrRuntimeFinalized
275276
}
276277

277-
for _, hooksDir := range hooksDirs {
278-
if hooksDir == "" {
279-
return fmt.Errorf("empty-string hook directories are not supported: %w", define.ErrInvalidArg)
280-
}
278+
if slices.Contains(hooksDirs, "") {
279+
return fmt.Errorf("empty-string hook directories are not supported: %w", define.ErrInvalidArg)
281280
}
282281

283282
rt.config.Engine.HooksDir.Set(hooksDirs)

libpod/plugin/volume_api.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"path/filepath"
13+
"slices"
1314
"strings"
1415
"sync"
1516
"time"
@@ -106,13 +107,7 @@ func validatePlugin(newPlugin *VolumePlugin) error {
106107
return fmt.Errorf("unmarshalling plugin %s activation response: %w", newPlugin.Name, err)
107108
}
108109

109-
foundVolume := false
110-
for _, pluginType := range respStruct.Implements {
111-
if pluginType == volumePluginType {
112-
foundVolume = true
113-
break
114-
}
115-
}
110+
foundVolume := slices.Contains(respStruct.Implements, volumePluginType)
116111

117112
if !foundVolume {
118113
return fmt.Errorf("plugin %s does not implement volume plugin, instead provides %s: %w", newPlugin.Name, strings.Join(respStruct.Implements, ", "), ErrNotVolumePlugin)

libpod/runtime_pod.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ func (r *Runtime) PrunePods(ctx context.Context) (map[string]error, error) {
166166
states := []string{define.PodStateStopped, define.PodStateExited}
167167
filterFunc := func(p *Pod) bool {
168168
state, _ := p.GetPodStatus()
169-
for _, status := range states {
170-
if state == status {
171-
return true
172-
}
173-
}
174-
return false
169+
return slices.Contains(states, state)
175170
}
176171
pods, err := r.Pods(filterFunc)
177172
if err != nil {

pkg/domain/filters/containers.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
5555
return func(c *libpod.Container) bool {
5656
ec, exited, err := c.ExitCode()
5757
if err == nil && exited {
58-
for _, exitCode := range exitCodes {
59-
if ec == exitCode {
60-
return true
61-
}
58+
if slices.Contains(exitCodes, ec) {
59+
return true
6260
}
6361
}
6462
return false
@@ -179,12 +177,7 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
179177
if err != nil {
180178
return false
181179
}
182-
for _, filterValue := range filterValues {
183-
if hcStatus == filterValue {
184-
return true
185-
}
186-
}
187-
return false
180+
return slices.Contains(filterValues, hcStatus)
188181
}, nil
189182
case "until":
190183
return prepareUntilFilterFunc(filterValues)
@@ -470,10 +463,8 @@ func GenerateExternalContainerFilterFuncs(filter string, filterValues []string,
470463
ec := listContainer.ExitCode
471464
exited := listContainer.Exited
472465
if exited {
473-
for _, exitCode := range exitCodes {
474-
if ec == exitCode {
475-
return true
476-
}
466+
if slices.Contains(exitCodes, ec) {
467+
return true
477468
}
478469
}
479470
return false

pkg/domain/filters/pods.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
111111
if err != nil {
112112
return false
113113
}
114-
for _, filterValue := range filterValues {
115-
if strings.ToLower(status) == filterValue {
116-
return true
117-
}
118-
}
119-
return false
114+
return slices.Contains(filterValues, strings.ToLower(status))
120115
}, nil
121116
case "label":
122117
return func(p *libpod.Pod) bool {

0 commit comments

Comments
 (0)