From 84a3905f61648eb0f9977b909bc0aea93bde4185 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 7 Apr 2025 12:23:13 -0700 Subject: [PATCH] Use slices.Contains Signed-off-by: Kir Kolyshkin --- chroot/run_test.go | 6 ++---- imagebuildah/stage_executor.go | 9 +-------- internal/sbom/presets.go | 12 +++++++----- new.go | 6 ++---- pkg/cli/build.go | 6 ++---- util/util.go | 8 ++------ 6 files changed, 16 insertions(+), 31 deletions(-) diff --git a/chroot/run_test.go b/chroot/run_test.go index 82651a467..3484cc06d 100644 --- a/chroot/run_test.go +++ b/chroot/run_test.go @@ -221,10 +221,8 @@ func TestProcessEnv(t *testing.T) { g.AddProcessEnv("PARENT_TEST_PID", strconv.Itoa(unix.Getpid())) }, func(t *testing.T, report *types.TestReport) { - for _, ev := range report.Spec.Process.Env { - if ev == e { - return - } + if slices.Contains(report.Spec.Process.Env, e) { + return } t.Fatalf("expected environment variable %q", e) }, diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index 7a9d5e210..0ec0ef047 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -173,14 +173,7 @@ func (s *StageExecutor) Preserve(path string) error { for cachedPath := range s.volumeCache { // Walk our list of cached volumes, and check that they're // still in the list of locations that we need to cache. - found := false - for _, volume := range s.volumes { - if volume == cachedPath { - // We need to keep this volume's cache. - found = true - break - } - } + found := slices.Contains(s.volumes, cachedPath) if !found { // We don't need to keep this volume's cache. Make a // note to remove it. diff --git a/internal/sbom/presets.go b/internal/sbom/presets.go index 8f104139a..43ea9a814 100644 --- a/internal/sbom/presets.go +++ b/internal/sbom/presets.go @@ -1,6 +1,10 @@ package sbom -import "github.com/containers/buildah/define" +import ( + "slices" + + "github.com/containers/buildah/define" +) // Preset returns a predefined SBOMScanOptions structure that has the passed-in // name as one of its "Type" values. @@ -55,10 +59,8 @@ func Preset(name string) (preset *define.SBOMScanOptions, err error) { }, } for _, preset := range presets { - for _, presetName := range preset.Type { - if presetName == name { - return &preset, nil - } + if slices.Contains(preset.Type, name) { + return &preset, nil } } return nil, nil diff --git a/new.go b/new.go index 465cc73d3..3bb4ff77f 100644 --- a/new.go +++ b/new.go @@ -99,10 +99,8 @@ func newContainerIDMappingOptions(idmapOptions *define.IDMappingOptions) storage func containerNameExist(name string, containers []storage.Container) bool { for _, container := range containers { - for _, cname := range container.Names { - if cname == name { - return true - } + if slices.Contains(container.Names, name) { + return true } } return false diff --git a/pkg/cli/build.go b/pkg/cli/build.go index 5437f1d36..ee4961a78 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -68,10 +68,8 @@ func GenBuildOptions(c *cobra.Command, inputArgs []string, iopts BuildOptions) ( tags = tags[1:] } if c.Flag("manifest").Changed { - for _, tag := range tags { - if tag == iopts.Manifest { - return options, nil, nil, errors.New("the same name must not be specified for both '--tag' and '--manifest'") - } + if slices.Contains(tags, iopts.Manifest) { + return options, nil, nil, errors.New("the same name must not be specified for both '--tag' and '--manifest'") } } } diff --git a/util/util.go b/util/util.go index cdbea01c9..6a3b2fc6b 100644 --- a/util/util.go +++ b/util/util.go @@ -335,12 +335,8 @@ func logIfNotErrno(err error, what string, ignores ...syscall.Errno) (logged boo if err == nil { return false } - if errno, isErrno := err.(syscall.Errno); isErrno { - for _, ignore := range ignores { - if errno == ignore { - return false - } - } + if errno, ok := err.(syscall.Errno); ok && slices.Contains(ignores, errno) { + return false } logrus.Error(what) return true