Skip to content

Commit 0829903

Browse files
authored
refactor: use slices.Contains to simplify code (#13076)
There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read. Signed-off-by: CoolCu <coolcui@qq.com>
1 parent 2f9c021 commit 0829903

File tree

6 files changed

+15
-49
lines changed

6 files changed

+15
-49
lines changed

api/miner_subsystems.go

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

33
import (
44
"encoding/json"
5+
"slices"
56
)
67

78
// MinerSubsystem represents a miner subsystem. Int and string values are not
@@ -57,12 +58,7 @@ func (ms *MinerSubsystem) UnmarshalJSON(b []byte) error {
5758
type MinerSubsystems []MinerSubsystem
5859

5960
func (ms MinerSubsystems) Has(entry MinerSubsystem) bool {
60-
for _, v := range ms {
61-
if v == entry {
62-
return true
63-
}
64-
}
65-
return false
61+
return slices.Contains(ms, entry)
6662
}
6763

6864
func (ms MinerSubsystem) String() string {

build/params_shared_funcs.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package build
22

33
import (
44
"os"
5+
"slices"
56
"strconv"
67
"strings"
78

@@ -107,22 +108,12 @@ func parseF3DisableActivationEnv() (contractAddrs []string, epochs []int64) {
107108
// epoch number based on environment variable configuration.
108109
func IsF3EpochActivationDisabled(epoch int64) bool {
109110
_, epochs := parseF3DisableActivationEnv()
110-
for _, e := range epochs {
111-
if e == epoch {
112-
return true
113-
}
114-
}
115-
return false
111+
return slices.Contains(epochs, epoch)
116112
}
117113

118114
// IsF3ContractActivationDisabled checks if F3 activation is disabled for the given contract address
119115
// based on environment variable configuration.
120116
func IsF3ContractActivationDisabled(contract string) bool {
121117
contracts, _ := parseF3DisableActivationEnv()
122-
for _, c := range contracts {
123-
if c == contract {
124-
return true
125-
}
126-
}
127-
return false
118+
return slices.Contains(contracts, contract)
128119
}

chain/events/filter/event.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"math"
7+
"slices"
78
"sync"
89
"time"
910

@@ -184,12 +185,7 @@ func (f *eventFilter) matchAddress(o address.Address) bool {
184185

185186
// Assume short lists of addresses
186187
// TODO: binary search for longer lists or restrict list length
187-
for _, a := range f.addresses {
188-
if a == o {
189-
return true
190-
}
191-
}
192-
return false
188+
return slices.Contains(f.addresses, o)
193189
}
194190

195191
func (f *eventFilter) matchKeys(ees []types.EventEntry) bool {

chain/types/tipset.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,7 @@ func (ts *TipSet) ParentWeight() BigInt {
238238
}
239239

240240
func (ts *TipSet) Contains(oc cid.Cid) bool {
241-
for _, c := range ts.cids {
242-
if c == oc {
243-
return true
244-
}
245-
}
246-
return false
241+
return slices.Contains(ts.cids, oc)
247242
}
248243

249244
func (ts *TipSet) IsChildOf(parent *TipSet) bool {

cmd/ci/main.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"os"
66
"path/filepath"
7+
"slices"
78
"strings"
89

910
log "github.com/sirupsen/logrus"
@@ -247,7 +248,7 @@ func getHasVeryExpensiveTests(testGroupName string) bool {
247248
testGroupNames := []string{
248249
"itest-niporep_manual",
249250
}
250-
return contains(testGroupNames, testGroupName)
251+
return slices.Contains(testGroupNames, testGroupName)
251252
}
252253

253254
func getTestGroupMetadata(testGroupName string) TestGroupMetadata {
@@ -327,21 +328,21 @@ func getNeedsParameters(testGroupName string) bool {
327328
"unit-cli",
328329
"unit-storage",
329330
}
330-
return contains(testGroupNames, testGroupName)
331+
return slices.Contains(testGroupNames, testGroupName)
331332
}
332333

333334
func getSkipConformance(testGroupName string) bool {
334335
testGroupNames := []string{
335336
"conformance",
336337
}
337-
return !contains(testGroupNames, testGroupName)
338+
return !slices.Contains(testGroupNames, testGroupName)
338339
}
339340

340341
func getTestRustProofLogs(testGroupName string) bool {
341342
testGroupNames := []string{
342343
"multicore-sdr",
343344
}
344-
return contains(testGroupNames, testGroupName)
345+
return slices.Contains(testGroupNames, testGroupName)
345346
}
346347

347348
func getFormat() string {
@@ -362,12 +363,3 @@ func getGoTestFlags(testGroupName string) string {
362363
func createPackagePath(pathParts ...string) string {
363364
return strings.Join(append([]string{"."}, pathParts...), string(os.PathSeparator))
364365
}
365-
366-
func contains(slice []string, item string) bool {
367-
for _, s := range slice {
368-
if s == item {
369-
return true
370-
}
371-
}
372-
return false
373-
}

cmd/release/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"regexp"
13+
"slices"
1314
"strconv"
1415
"strings"
1516

@@ -94,12 +95,7 @@ func getBinaries(name string) []string {
9495

9596
func isReleased(tag string) bool {
9697
tags := getTags()
97-
for _, t := range tags {
98-
if t == tag {
99-
return true
100-
}
101-
}
102-
return false
98+
return slices.Contains(tags, tag)
10399
}
104100

105101
func getPrefix(name string) string {

0 commit comments

Comments
 (0)