Skip to content

Commit e16f35e

Browse files
gammazerosukunrt
andauthored
refactor: apply go fix modernizers from Go 1.26 (#3463)
Co-authored-by: sukun <sukunrt@gmail.com>
1 parent 8a6fd5b commit e16f35e

File tree

131 files changed

+553
-631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+553
-631
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ run:
66
issues:
77
max-issues-per-linter: 0
88
max-same-issues: 0
9+
exclude-rules:
10+
- path: _test\.go
11+
linters:
12+
- prealloc
913

1014
linters:
1115
enable:

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type AutoNATConfig struct {
7474

7575
type Security struct {
7676
ID protocol.ID
77-
Constructor interface{}
77+
Constructor any
7878
}
7979

8080
// Config describes a set of settings for a libp2p node

core/discovery/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Options struct {
1111
Limit int
1212

1313
// Other (implementation-specific) options
14-
Other map[interface{}]interface{}
14+
Other map[any]any
1515
}
1616

1717
// Apply applies the given options to this DiscoveryOpts

core/event/bus.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import (
66
)
77

88
// SubscriptionOpt represents a subscriber option. Use the options exposed by the implementation of choice.
9-
type SubscriptionOpt = func(interface{}) error
9+
type SubscriptionOpt = func(any) error
1010

1111
// EmitterOpt represents an emitter option. Use the options exposed by the implementation of choice.
12-
type EmitterOpt = func(interface{}) error
12+
type EmitterOpt = func(any) error
1313

1414
// CancelFunc closes a subscriber.
1515
type CancelFunc = func()
1616

1717
// wildcardSubscriptionType is a virtual type to represent wildcard
1818
// subscriptions.
19-
type wildcardSubscriptionType interface{}
19+
type wildcardSubscriptionType any
2020

2121
// WildcardSubscription is the type to subscribe to receive all events
2222
// emitted in the eventbus.
@@ -30,15 +30,15 @@ type Emitter interface {
3030
// calls to Emit will block.
3131
//
3232
// Calling this function with wrong event type will cause a panic.
33-
Emit(evt interface{}) error
33+
Emit(evt any) error
3434
}
3535

3636
// Subscription represents a subscription to one or multiple event types.
3737
type Subscription interface {
3838
io.Closer
3939

4040
// Out returns the channel from which to consume events.
41-
Out() <-chan interface{}
41+
Out() <-chan any
4242

4343
// Name returns the name for the subscription
4444
Name() string
@@ -79,7 +79,7 @@ type Bus interface {
7979
// [...]
8080
// }
8181
// }
82-
Subscribe(eventType interface{}, opts ...SubscriptionOpt) (Subscription, error)
82+
Subscribe(eventType any, opts ...SubscriptionOpt) (Subscription, error)
8383

8484
// Emitter creates a new event emitter.
8585
//
@@ -89,7 +89,7 @@ type Bus interface {
8989
// em, err := eventbus.Emitter(new(EventT))
9090
// defer em.Close() // MUST call this after being done with the emitter
9191
// em.Emit(EventT{})
92-
Emitter(eventType interface{}, opts ...EmitterOpt) (Emitter, error)
92+
Emitter(eventType any, opts ...EmitterOpt) (Emitter, error)
9393

9494
// GetAllEventTypes returns all the event types that this bus knows about
9595
// (having emitters and subscribers). It omits the WildcardSubscription.

core/internal/catch/catch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
var panicWriter io.Writer = os.Stderr
1111

1212
// HandlePanic handles and logs panics.
13-
func HandlePanic(rerr interface{}, err *error, where string) {
13+
func HandlePanic(rerr any, err *error, where string) {
1414
if rerr != nil {
1515
fmt.Fprintf(panicWriter, "caught panic: %s\n%s\n", rerr, debug.Stack())
1616
*err = fmt.Errorf("panic in %s: %s", where, rerr)

core/metrics/bandwidth_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ func round(bwc *BandwidthCounter, b *testing.B) {
3535
start := make(chan struct{})
3636
var wg sync.WaitGroup
3737
wg.Add(10000)
38-
for i := 0; i < 1000; i++ {
38+
for i := range 1000 {
3939
p := peer.ID(fmt.Sprintf("peer-%d", i))
40-
for j := 0; j < 10; j++ {
40+
for j := range 10 {
4141
proto := protocol.ID(fmt.Sprintf("bitswap-%d", j))
4242
go func() {
4343
defer wg.Done()
4444
<-start
4545

46-
for i := 0; i < 1000; i++ {
46+
for range 1000 {
4747
bwc.LogSentMessage(100)
4848
bwc.LogSentMessageStream(100, proto, p)
4949
time.Sleep(1 * time.Millisecond)
@@ -60,10 +60,10 @@ func round(bwc *BandwidthCounter, b *testing.B) {
6060

6161
func TestBandwidthCounter(t *testing.T) {
6262
bwc := NewBandwidthCounter()
63-
for i := 0; i < 40; i++ {
64-
for i := 0; i < 100; i++ {
63+
for range 40 {
64+
for i := range 100 {
6565
p := peer.ID(fmt.Sprintf("peer-%d", i))
66-
for j := 0; j < 2; j++ {
66+
for j := range 2 {
6767
proto := protocol.ID(fmt.Sprintf("proto-%d", j))
6868

6969
// make sure the bandwidth counters are active
@@ -81,7 +81,7 @@ func TestBandwidthCounter(t *testing.T) {
8181
assertProtocols := func(check func(Stats)) {
8282
byProtocol := bwc.GetBandwidthByProtocol()
8383
require.Len(t, byProtocol, 2, "expected 2 protocols")
84-
for i := 0; i < 2; i++ {
84+
for i := range 2 {
8585
p := protocol.ID(fmt.Sprintf("proto-%d", i))
8686
for _, stats := range [...]Stats{bwc.GetBandwidthForProtocol(p), byProtocol[p]} {
8787
check(stats)
@@ -92,7 +92,7 @@ func TestBandwidthCounter(t *testing.T) {
9292
assertPeers := func(check func(Stats)) {
9393
byPeer := bwc.GetBandwidthByPeer()
9494
require.Len(t, byPeer, 100, "expected 100 peers")
95-
for i := 0; i < 100; i++ {
95+
for i := range 100 {
9696
p := peer.ID(fmt.Sprintf("peer-%d", i))
9797
for _, stats := range [...]Stats{bwc.GetBandwidthForPeer(p), byPeer[p]} {
9898
check(stats)

core/network/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type Stats struct {
123123
// relay.
124124
Limited bool
125125
// Extra stores additional metadata about this connection.
126-
Extra map[interface{}]interface{}
126+
Extra map[any]any
127127
}
128128

129129
// StreamHandler is the type of function used to listen for

core/peer/addrinfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func AddrInfoToP2pAddrs(pi *AddrInfo) ([]ma.Multiaddr, error) {
118118
return addrs, nil
119119
}
120120

121-
func (pi *AddrInfo) Loggable() map[string]interface{} {
122-
return map[string]interface{}{
121+
func (pi *AddrInfo) Loggable() map[string]any {
122+
return map[string]any{
123123
"peerID": pi.ID.String(),
124124
"addrs": pi.Addrs,
125125
}

core/peer/peer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const maxInlineKeyLength = 42
4242
type ID string
4343

4444
// Loggable returns a pretty peer ID string in loggable JSON format.
45-
func (id ID) Loggable() map[string]interface{} {
46-
return map[string]interface{}{
45+
func (id ID) Loggable() map[string]any {
46+
return map[string]any{
4747
"peerID": id.String(),
4848
}
4949
}

core/peer/record_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestSignedPeerRecordFromEnvelope(t *testing.T) {
5757
// low clock precision. This makes sure we never get a duplicate.
5858
func TestTimestampSeq(t *testing.T) {
5959
var last uint64
60-
for i := 0; i < 1000; i++ {
60+
for range 1000 {
6161
next := TimestampSeq()
6262
if next <= last {
6363
t.Errorf("non-increasing timestamp found: %d <= %d", next, last)

0 commit comments

Comments
 (0)