Skip to content

Commit 39ceaf7

Browse files
cuishuanggopherbot
authored andcommitted
all: use slices.Contains to simplify code
Change-Id: I9ef075bbb0e3c65f3c2a9d49e599ef50b18aa9be Reviewed-on: https://go-review.googlesource.com/c/go/+/639535 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 3caf5bd commit 39ceaf7

File tree

9 files changed

+18
-75
lines changed

9 files changed

+18
-75
lines changed

src/cmd/dist/test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"reflect"
1919
"regexp"
2020
"runtime"
21+
"slices"
2122
"strconv"
2223
"strings"
2324
"time"
@@ -280,12 +281,7 @@ func (t *tester) shouldRunTest(name string) bool {
280281
if len(t.runNames) == 0 {
281282
return true
282283
}
283-
for _, runName := range t.runNames {
284-
if runName == name {
285-
return true
286-
}
287-
}
288-
return false
284+
return slices.Contains(t.runNames, name)
289285
}
290286

291287
func (t *tester) maybeLogMetadata() error {

src/cmd/go/internal/modindex/build.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"io/fs"
2323
"path/filepath"
24+
"slices"
2425
"sort"
2526
"strings"
2627
"unicode"
@@ -887,23 +888,8 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
887888
}
888889

889890
// other tags
890-
for _, tag := range ctxt.BuildTags {
891-
if tag == name {
892-
return true
893-
}
894-
}
895-
for _, tag := range ctxt.ToolTags {
896-
if tag == name {
897-
return true
898-
}
899-
}
900-
for _, tag := range ctxt.ReleaseTags {
901-
if tag == name {
902-
return true
903-
}
904-
}
905-
906-
return false
891+
return slices.Contains(ctxt.BuildTags, name) || slices.Contains(ctxt.ToolTags, name) ||
892+
slices.Contains(ctxt.ReleaseTags, name)
907893
}
908894

909895
// goodOSArchFile returns false if the name contains a $GOOS or $GOARCH

src/crypto/tls/handshake_server_tls13.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,7 @@ func (hs *serverHandshakeStateTLS13) shouldSendSessionTickets() bool {
949949
}
950950

951951
// Don't send tickets the client wouldn't use. See RFC 8446, Section 4.2.9.
952-
for _, pskMode := range hs.clientHello.pskModes {
953-
if pskMode == pskModeDHE {
954-
return true
955-
}
956-
}
957-
return false
952+
return slices.Contains(hs.clientHello.pskModes, pskModeDHE)
958953
}
959954

960955
func (hs *serverHandshakeStateTLS13) sendSessionTickets() error {

src/go/build/build.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,23 +1985,8 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
19851985
}
19861986

19871987
// other tags
1988-
for _, tag := range ctxt.BuildTags {
1989-
if tag == name {
1990-
return true
1991-
}
1992-
}
1993-
for _, tag := range ctxt.ToolTags {
1994-
if tag == name {
1995-
return true
1996-
}
1997-
}
1998-
for _, tag := range ctxt.ReleaseTags {
1999-
if tag == name {
2000-
return true
2001-
}
2002-
}
2003-
2004-
return false
1988+
return slices.Contains(ctxt.BuildTags, name) || slices.Contains(ctxt.ToolTags, name) ||
1989+
slices.Contains(ctxt.ReleaseTags, name)
20051990
}
20061991

20071992
// goodOSArchFile returns false if the name contains a $GOOS or $GOARCH

src/internal/reflectlite/reflect_mirror_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path/filepath"
1515
"runtime"
16+
"slices"
1617
"strings"
1718
"sync"
1819
"testing"
@@ -40,12 +41,7 @@ func newVisitor() visitor {
4041
return v
4142
}
4243
func (v visitor) filter(name string) bool {
43-
for _, typeName := range typeNames {
44-
if typeName == name {
45-
return true
46-
}
47-
}
48-
return false
44+
return slices.Contains(typeNames, name)
4945
}
5046

5147
func (v visitor) Visit(n ast.Node) ast.Visitor {

src/internal/trace/order.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package trace
66

77
import (
88
"fmt"
9+
"slices"
910
"strings"
1011

1112
"internal/trace/event"
@@ -1254,12 +1255,7 @@ func (s *rangeState) activeRange(typ rangeType, isInitialGen bool) error {
12541255

12551256
// hasRange returns true if a special time range on the goroutine as in progress.
12561257
func (s *rangeState) hasRange(typ rangeType) bool {
1257-
for _, ftyp := range s.inFlight {
1258-
if ftyp == typ {
1259-
return true
1260-
}
1261-
}
1262-
return false
1258+
return slices.Contains(s.inFlight, typ)
12631259
}
12641260

12651261
// endRange ends a special range in time on the goroutine.

src/os/user/user_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package user
66

77
import (
88
"os"
9+
"slices"
910
"testing"
1011
)
1112

@@ -178,16 +179,7 @@ func TestGroupIds(t *testing.T) {
178179
if err != nil {
179180
t.Fatalf("%+v.GroupIds(): %v", user, err)
180181
}
181-
if !containsID(gids, user.Gid) {
182+
if !slices.Contains(gids, user.Gid) {
182183
t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
183184
}
184185
}
185-
186-
func containsID(ids []string, id string) bool {
187-
for _, x := range ids {
188-
if x == id {
189-
return true
190-
}
191-
}
192-
return false
193-
}

src/os/user/user_windows_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os"
1515
"os/exec"
1616
"runtime"
17+
"slices"
1718
"strconv"
1819
"syscall"
1920
"testing"
@@ -205,7 +206,7 @@ func TestGroupIdsTestUser(t *testing.T) {
205206
if err != nil {
206207
t.Fatalf("%+v.GroupIds(): %v", user, err)
207208
}
208-
if !containsID(gids, user.Gid) {
209+
if !slices.Contains(gids, user.Gid) {
209210
t.Errorf("%+v.GroupIds() = %v; does not contain user GID %s", user, gids, user.Gid)
210211
}
211212
}

src/syscall/syscall_linux.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"internal/itoa"
1616
runtimesyscall "internal/runtime/syscall"
1717
"runtime"
18+
"slices"
1819
"unsafe"
1920
)
2021

@@ -134,12 +135,7 @@ func isGroupMember(gid int) bool {
134135
return false
135136
}
136137

137-
for _, g := range groups {
138-
if g == gid {
139-
return true
140-
}
141-
}
142-
return false
138+
return slices.Contains(groups, gid)
143139
}
144140

145141
func isCapDacOverrideSet() bool {

0 commit comments

Comments
 (0)