Repository: libp2p/go-libp2p
Pull request #2680 (2024-01-03 20:40:14)

chore: remove unnecessary conversions
Used linter to detect unnecessary type conversions
https://github.com/mdempsky/unconvert


Patch:
diff --git a/options.go b/options.go
index 1a1e9d39..747d6c55 100644
--- a/options.go
+++ b/options.go
@@ -349,7 +349,7 @@ func EnableAutoRelayWithPeerSource(peerSource autorelay.PeerSource, opts ...auto
 // forcing the local node to believe it is reachable externally.
 func ForceReachabilityPublic() Option {
 	return func(cfg *Config) error {
-		public := network.Reachability(network.ReachabilityPublic)
+		public := network.ReachabilityPublic
 		cfg.AutoNATConfig.ForceReachability = &public
 		return nil
 	}
@@ -359,7 +359,7 @@ func ForceReachabilityPublic() Option {
 // forceing the local node to believe it is behind a NAT and not reachable externally.
 func ForceReachabilityPrivate() Option {
 	return func(cfg *Config) error {
-		private := network.Reachability(network.ReachabilityPrivate)
+		private := network.ReachabilityPrivate
 		cfg.AutoNATConfig.ForceReachability = &private
 		return nil
 	}
diff --git a/p2p/host/peerstore/pstoreds/protobook.go b/p2p/host/peerstore/pstoreds/protobook.go
index f5d76573..40fa7d95 100644
--- a/p2p/host/peerstore/pstoreds/protobook.go
+++ b/p2p/host/peerstore/pstoreds/protobook.go
@@ -17,7 +17,7 @@ type protoSegment struct {
 type protoSegments [256]*protoSegment
 
 func (s *protoSegments) get(p peer.ID) *protoSegment {
-	return s[byte(p[len(p)-1])]
+	return s[p[len(p)-1]]
 }
 
 var errTooManyProtocols = errors.New("too many protocols")
diff --git a/p2p/host/peerstore/pstoremem/addr_book.go b/p2p/host/peerstore/pstoremem/addr_book.go
index 67f9f914..209937ca 100644
--- a/p2p/host/peerstore/pstoremem/addr_book.go
+++ b/p2p/host/peerstore/pstoremem/addr_book.go
@@ -49,7 +49,7 @@ func (segments *addrSegments) get(p peer.ID) *addrSegment {
 	if len(p) == 0 { // it's not terribly useful to use an empty peer ID, but at least we should not panic
 		return segments[0]
 	}
-	return segments[uint8(p[len(p)-1])]
+	return segments[p[len(p)-1]]
 }
 
 type clock interface {
diff --git a/p2p/host/peerstore/pstoremem/protobook.go b/p2p/host/peerstore/pstoremem/protobook.go
index 0000f97f..51c4b028 100644
--- a/p2p/host/peerstore/pstoremem/protobook.go
+++ b/p2p/host/peerstore/pstoremem/protobook.go
@@ -17,7 +17,7 @@ type protoSegment struct {
 type protoSegments [256]*protoSegment
 
 func (s *protoSegments) get(p peer.ID) *protoSegment {
-	return s[byte(p[len(p)-1])]
+	return s[p[len(p)-1]]
 }
 
 var errTooManyProtocols = errors.New("too many protocols")
diff --git a/p2p/net/connmgr/connmgr.go b/p2p/net/connmgr/connmgr.go
index 6caa3dc1..cf6bb58e 100644
--- a/p2p/net/connmgr/connmgr.go
+++ b/p2p/net/connmgr/connmgr.go
@@ -73,7 +73,7 @@ type segments struct {
 }
 
 func (ss *segments) get(p peer.ID) *segment {
-	return ss.buckets[byte(p[len(p)-1])]
+	return ss.buckets[p[len(p)-1]]
 }
 
 func (ss *segments) countPeers() (count int) {
diff --git a/p2p/net/swarm/swarm_metrics.go b/p2p/net/swarm/swarm_metrics.go
index 9cc34599..b5c0f2e4 100644
--- a/p2p/net/swarm/swarm_metrics.go
+++ b/p2p/net/swarm/swarm_metrics.go
@@ -166,7 +166,7 @@ func appendConnectionState(tags []string, cs network.ConnectionState) []string {
 		// This shouldn't happen, unless the transport doesn't properly set the Transport field in the ConnectionState.
 		tags = append(tags, "unknown")
 	} else {
-		tags = append(tags, string(cs.Transport))
+		tags = append(tags, cs.Transport)
 	}
 	// These might be empty, depending on the transport.
 	// For example, QUIC doesn't set security nor muxer.


Test patch:
diff --git a/p2p/host/resource-manager/scope_test.go b/p2p/host/resource-manager/scope_test.go
index 0464cb0c..abef4762 100644
--- a/p2p/host/resource-manager/scope_test.go
+++ b/p2p/host/resource-manager/scope_test.go
@@ -97,7 +97,7 @@ func TestCheckMemory(t *testing.T) {
 			return true
 		}
 
-		return (err != nil) == (uint64(res)+uint64(rc.memory) > (uint64(limit) >> uint64(8-priShift)))
+		return (err != nil) == (res+uint64(rc.memory) > (limit >> uint64(8-priShift)))
 	}
 
 	require.NoError(t, quick.Check(f, nil))
diff --git a/p2p/net/gostream/gostream_test.go b/p2p/net/gostream/gostream_test.go
index fc6ab837..c024cabc 100644
--- a/p2p/net/gostream/gostream_test.go
+++ b/p2p/net/gostream/gostream_test.go
@@ -129,7 +129,7 @@ func TestServerClient(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	if string(resp) != "yes it is\n" {
+	if resp != "yes it is\n" {
 		t.Errorf("Bad response: %s", resp)
 	}
 
diff --git a/p2p/net/mock/mock_test.go b/p2p/net/mock/mock_test.go
index 863e54f1..aed12548 100644
--- a/p2p/net/mock/mock_test.go
+++ b/p2p/net/mock/mock_test.go
@@ -463,11 +463,11 @@ func TestRateLimiting(t *testing.T) {
 	}
 
 	rl.UpdateBandwidth(100)
-	if !within(rl.Limit(1), time.Duration(time.Millisecond*10), time.Millisecond) {
+	if !within(rl.Limit(1), time.Millisecond*10, time.Millisecond) {
 		t.Fatal()
 	}
 
-	if within(rl.Limit(1), time.Duration(time.Millisecond*10), time.Millisecond) {
+	if within(rl.Limit(1), time.Millisecond*10, time.Millisecond) {
 		t.Fatal()
 	}
 }
diff --git a/p2p/transport/webtransport/cert_manager_test.go b/p2p/transport/webtransport/cert_manager_test.go
index 29999a8f..942d4717 100644
--- a/p2p/transport/webtransport/cert_manager_test.go
+++ b/p2p/transport/webtransport/cert_manager_test.go
@@ -166,7 +166,7 @@ func TestGetCurrentBucketStartTimeIsWithinBounds(t *testing.T) {
 
 		offset = offset % certValidity
 		// Bound this to 100 years
-		timeSinceUnixEpoch = time.Duration(timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100))
+		timeSinceUnixEpoch = timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100)
 		// Start a bit further in the future to avoid edge cases around epoch
 		timeSinceUnixEpoch += time.Hour * 24 * 365
 		start := time.UnixMilli(timeSinceUnixEpoch.Milliseconds())
diff --git a/p2p/transport/webtransport/transport_test.go b/p2p/transport/webtransport/transport_test.go
index 7b452403..f6c850a2 100644
--- a/p2p/transport/webtransport/transport_test.go
+++ b/p2p/transport/webtransport/transport_test.go
@@ -650,7 +650,7 @@ func serverSendsBackValidCert(t *testing.T, timeSinceUnixEpoch time.Duration, ke
 	}
 
 	// Bound this to 100 years
-	timeSinceUnixEpoch = time.Duration(timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100))
+	timeSinceUnixEpoch = timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100)
 	// Start a bit further in the future to avoid edge cases around epoch
 	timeSinceUnixEpoch += time.Hour * 24 * 365
 	start := time.UnixMilli(timeSinceUnixEpoch.Milliseconds())
@@ -729,7 +729,7 @@ func TestServerRotatesCertCorrectly(t *testing.T) {
 		}
 
 		// Bound this to 100 years
-		timeSinceUnixEpoch = time.Duration(timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100))
+		timeSinceUnixEpoch = timeSinceUnixEpoch % (time.Hour * 24 * 365 * 100)
 		// Start a bit further in the future to avoid edge cases around epoch
 		timeSinceUnixEpoch += time.Hour * 24 * 365
 		start := time.UnixMilli(timeSinceUnixEpoch.Milliseconds())