Skip to content

Commit 704f501

Browse files
authored
lint: run modernize slicescontains,sortslice,stringscutprefix (#6433)
1 parent 585b8d2 commit 704f501

File tree

7 files changed

+19
-36
lines changed

7 files changed

+19
-36
lines changed

agreement/credentialArrivalHistory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package agreement
1818

1919
import (
20-
"sort"
20+
"slices"
2121
"time"
2222
)
2323

@@ -78,6 +78,6 @@ func (history *credentialArrivalHistory) orderStatistics(idx int) time.Duration
7878
// the linear time order statistics algorithm.
7979
sortedArrivals := make([]time.Duration, len(history.history))
8080
copy(sortedArrivals[:], history.history[:])
81-
sort.Slice(sortedArrivals, func(i, j int) bool { return sortedArrivals[i] < sortedArrivals[j] })
81+
slices.Sort(sortedArrivals)
8282
return sortedArrivals[idx]
8383
}

agreement/state_machine_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"os"
23+
"slices"
2324
"strings"
2425

2526
"github.com/algorand/go-algorand/logging"
@@ -158,12 +159,7 @@ func (t ioTrace) CountEvent(b event) (count int) {
158159

159160
// for each event, passes it into the given fn; if returns true, returns true.
160161
func (t ioTrace) ContainsFn(compareFn func(b event) bool) bool {
161-
for _, ev := range t.events {
162-
if compareFn(ev) {
163-
return true
164-
}
165-
}
166-
return false
162+
return slices.ContainsFunc(t.events, compareFn)
167163
}
168164

169165
func (t ioTrace) countAction() (count int) {

data/basics/fields_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package basics_test
1818

1919
import (
2020
"reflect"
21+
"slices"
2122
"testing"
2223

2324
"github.com/algorand/go-algorand/data/basics"
@@ -35,11 +36,9 @@ func makeTypeCheckFunction(t *testing.T, exceptions []reflectionhelpers.TypePath
3536
return func(path reflectionhelpers.TypePath, stack []reflect.Type) bool {
3637
currentType := stack[len(stack)-1]
3738

38-
for _, exception := range exceptions {
39-
if path.Equals(exception) {
40-
t.Logf("Skipping exception for path: %s", path)
41-
return true
42-
}
39+
if slices.ContainsFunc(exceptions, path.Equals) {
40+
t.Logf("Skipping exception for path: %s", path)
41+
return true
4342
}
4443

4544
switch currentType.Kind() {

gen/generate_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io"
2323
"os"
2424
"path/filepath"
25+
"slices"
2526
"strings"
2627
"sync"
2728
"testing"
@@ -171,12 +172,7 @@ func TestGenesisJsonCreation(t *testing.T) {
171172
deterministicAddresses := []string{"FeeSink", "RewardsPool"}
172173

173174
isNondeterministicAddress := func(name string) bool {
174-
for _, address := range deterministicAddresses {
175-
if name == address {
176-
return false
177-
}
178-
}
179-
return true
175+
return !slices.Contains(deterministicAddresses, name)
180176
}
181177

182178
for i := range as {

ledger/simulation/simulator_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package simulation
1818

1919
import (
2020
"reflect"
21+
"slices"
2122
"testing"
2223

2324
"github.com/algorand/go-algorand/crypto"
@@ -59,17 +60,10 @@ func TestNonOverridenDataLedgerMethodsUseRoundParameter(t *testing.T) {
5960
}
6061

6162
methodIsSkipped := func(methodName string) bool {
62-
for _, overridenMethod := range overridenMethods {
63-
if overridenMethod == methodName {
64-
return true
65-
}
66-
}
67-
for _, excludedMethod := range excludedMethods {
68-
if excludedMethod == methodName {
69-
return true
70-
}
63+
if slices.Contains(overridenMethods, methodName) {
64+
return true
7165
}
72-
return false
66+
return slices.Contains(excludedMethods, methodName)
7367
}
7468

7569
methodExistsInEvalLedger := func(methodName string) bool {

network/wsNetwork.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,11 +2392,9 @@ func (wn *WebsocketNetwork) addPeer(peer *wsPeer) {
23922392
}
23932393
// simple duplicate *pointer* check. should never trigger given the callers to addPeer
23942394
// TODO: remove this after making sure it is safe to do so
2395-
for _, p := range wn.peers {
2396-
if p == peer {
2397-
wn.log.Errorf("dup peer added %#v", peer)
2398-
return
2399-
}
2395+
if slices.Contains(wn.peers, peer) {
2396+
wn.log.Errorf("dup peer added %#v", peer)
2397+
return
24002398
}
24012399
heap.Push(peersHeap{wn}, peer)
24022400
wn.prioTracker.setPriority(peer, peer.prioAddress, peer.prioWeight)

test/e2e-go/features/catchup/catchpointCatchup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ func downloadCatchpointFile(t *testing.T, a *require.Assertions, baseURL string,
550550
var chunks []ledger.CatchpointSnapshotChunkV6
551551
for _, d := range tarData {
552552
t.Logf("tar filename: %s, size %d", d.headerName, len(d.data))
553-
if strings.HasPrefix(d.headerName, "balances.") { // chunk file
554-
idxStr := strings.TrimSuffix(strings.TrimPrefix(d.headerName, "balances."), ".msgpack")
553+
if after, ok := strings.CutPrefix(d.headerName, "balances."); ok { // chunk file
554+
idxStr := strings.TrimSuffix(after, ".msgpack")
555555
idx, err := strconv.Atoi(idxStr)
556556
a.NoError(err)
557557
var c ledger.CatchpointSnapshotChunkV6

0 commit comments

Comments
 (0)