]> Cypherpunks repositories - gostls13.git/commitdiff
all: use slices.Contains to simplify code
authorcuishuang <imcusg@gmail.com>
Tue, 31 Dec 2024 16:17:54 +0000 (00:17 +0800)
committerGopher Robot <gobot@golang.org>
Mon, 3 Feb 2025 16:47:55 +0000 (08:47 -0800)
Change-Id: I9ef075bbb0e3c65f3c2a9d49e599ef50b18aa9be
Reviewed-on: https://go-review.googlesource.com/c/go/+/639535
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/cmd/dist/test.go
src/cmd/go/internal/modindex/build.go
src/crypto/tls/handshake_server_tls13.go
src/go/build/build.go
src/internal/reflectlite/reflect_mirror_test.go
src/internal/trace/order.go
src/os/user/user_test.go
src/os/user/user_windows_test.go
src/syscall/syscall_linux.go

index 58e87f16c05024e8884db98f7e69398536293f63..ba273d79234b3a25567d4cbcfd0b035140253900 100644 (file)
@@ -18,6 +18,7 @@ import (
        "reflect"
        "regexp"
        "runtime"
+       "slices"
        "strconv"
        "strings"
        "time"
@@ -280,12 +281,7 @@ func (t *tester) shouldRunTest(name string) bool {
        if len(t.runNames) == 0 {
                return true
        }
-       for _, runName := range t.runNames {
-               if runName == name {
-                       return true
-               }
-       }
-       return false
+       return slices.Contains(t.runNames, name)
 }
 
 func (t *tester) maybeLogMetadata() error {
index 542d6fbbbba390d6afd4720763dc89b11977f5ad..d7e09fed25f43a2c4c934260cec8568db49160f3 100644 (file)
@@ -21,6 +21,7 @@ import (
        "io"
        "io/fs"
        "path/filepath"
+       "slices"
        "sort"
        "strings"
        "unicode"
@@ -887,23 +888,8 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
        }
 
        // other tags
-       for _, tag := range ctxt.BuildTags {
-               if tag == name {
-                       return true
-               }
-       }
-       for _, tag := range ctxt.ToolTags {
-               if tag == name {
-                       return true
-               }
-       }
-       for _, tag := range ctxt.ReleaseTags {
-               if tag == name {
-                       return true
-               }
-       }
-
-       return false
+       return slices.Contains(ctxt.BuildTags, name) || slices.Contains(ctxt.ToolTags, name) ||
+               slices.Contains(ctxt.ReleaseTags, name)
 }
 
 // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
index 76fff6974e74030a9b28a066ea2249b992f195a1..b6d455cd397e315e7b867b69ea346be700b406cb 100644 (file)
@@ -949,12 +949,7 @@ func (hs *serverHandshakeStateTLS13) shouldSendSessionTickets() bool {
        }
 
        // Don't send tickets the client wouldn't use. See RFC 8446, Section 4.2.9.
-       for _, pskMode := range hs.clientHello.pskModes {
-               if pskMode == pskModeDHE {
-                       return true
-               }
-       }
-       return false
+       return slices.Contains(hs.clientHello.pskModes, pskModeDHE)
 }
 
 func (hs *serverHandshakeStateTLS13) sendSessionTickets() error {
index 9ffffda08a99b12f29ba94d845b489004a1b5e78..0e5c7e512d794ccd29fa5995d35f1d44a641f50c 100644 (file)
@@ -1985,23 +1985,8 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
        }
 
        // other tags
-       for _, tag := range ctxt.BuildTags {
-               if tag == name {
-                       return true
-               }
-       }
-       for _, tag := range ctxt.ToolTags {
-               if tag == name {
-                       return true
-               }
-       }
-       for _, tag := range ctxt.ReleaseTags {
-               if tag == name {
-                       return true
-               }
-       }
-
-       return false
+       return slices.Contains(ctxt.BuildTags, name) || slices.Contains(ctxt.ToolTags, name) ||
+               slices.Contains(ctxt.ReleaseTags, name)
 }
 
 // goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
index c87573903454edb4b4e8f531c0f3aaebc25035a9..8d13641516341a28b65b269d22998809021f8206 100644 (file)
@@ -13,6 +13,7 @@ import (
        "os"
        "path/filepath"
        "runtime"
+       "slices"
        "strings"
        "sync"
        "testing"
@@ -40,12 +41,7 @@ func newVisitor() visitor {
        return v
 }
 func (v visitor) filter(name string) bool {
-       for _, typeName := range typeNames {
-               if typeName == name {
-                       return true
-               }
-       }
-       return false
+       return slices.Contains(typeNames, name)
 }
 
 func (v visitor) Visit(n ast.Node) ast.Visitor {
index d0818a500c03c8b7b14bb7d6242996cc2f8320ed..8a1261330175acea57183d7f858c3c59f8dc2199 100644 (file)
@@ -6,6 +6,7 @@ package trace
 
 import (
        "fmt"
+       "slices"
        "strings"
 
        "internal/trace/event"
@@ -1254,12 +1255,7 @@ func (s *rangeState) activeRange(typ rangeType, isInitialGen bool) error {
 
 // hasRange returns true if a special time range on the goroutine as in progress.
 func (s *rangeState) hasRange(typ rangeType) bool {
-       for _, ftyp := range s.inFlight {
-               if ftyp == typ {
-                       return true
-               }
-       }
-       return false
+       return slices.Contains(s.inFlight, typ)
 }
 
 // endRange ends a special range in time on the goroutine.
index 31486aed03381970896c417dc84a8f529bb305ac..0e06369bf5a5da98ce74cb603379ace738cdf54d 100644 (file)
@@ -6,6 +6,7 @@ package user
 
 import (
        "os"
+       "slices"
        "testing"
 )
 
@@ -178,16 +179,7 @@ func TestGroupIds(t *testing.T) {
        if err != nil {
                t.Fatalf("%+v.GroupIds(): %v", user, err)
        }
-       if !containsID(gids, user.Gid) {
+       if !slices.Contains(gids, user.Gid) {
                t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
        }
 }
-
-func containsID(ids []string, id string) bool {
-       for _, x := range ids {
-               if x == id {
-                       return true
-               }
-       }
-       return false
-}
index c71503372e01073fd8e4f16c4c4458f3b2b6c051..7dca2fc5f94a16c31925a668ab38c72aae0a1c94 100644 (file)
@@ -14,6 +14,7 @@ import (
        "os"
        "os/exec"
        "runtime"
+       "slices"
        "strconv"
        "syscall"
        "testing"
@@ -205,7 +206,7 @@ func TestGroupIdsTestUser(t *testing.T) {
        if err != nil {
                t.Fatalf("%+v.GroupIds(): %v", user, err)
        }
-       if !containsID(gids, user.Gid) {
+       if !slices.Contains(gids, user.Gid) {
                t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
        }
 }
index 003f7a538c581f35413d1bf36bd3635dc76c8663..57d84748fe56d78d8dfccf5291230543260a55a5 100644 (file)
@@ -15,6 +15,7 @@ import (
        "internal/itoa"
        runtimesyscall "internal/runtime/syscall"
        "runtime"
+       "slices"
        "unsafe"
 )
 
@@ -134,12 +135,7 @@ func isGroupMember(gid int) bool {
                return false
        }
 
-       for _, g := range groups {
-               if g == gid {
-                       return true
-               }
-       }
-       return false
+       return slices.Contains(groups, gid)
 }
 
 func isCapDacOverrideSet() bool {