Skip to content
7 changes: 5 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ linters:
- unused
- prealloc

disable:
- errcheck

settings:
revive:
severity: warning
rules:
- name: unused-parameter
severity: warning
disabled: true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this disabled? This was the entire reason why we included revive.

See: #3269 (comment)

@galargh galargh Jun 3, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It produces a lot of errors at the moment. See https://github.com/libp2p/go-libp2p/actions/runs/15387329399/job/43288668727, for example (btw, 50 is the default cap on the number of reported issues). Do we want all these occurrences to be replaced with _? If so, I can take care of it and reenable the rule.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer enabling this particular check. It is useful.

I'm also happy with having the "only new changes" option for linting as we currently do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"only new changes" makes sense in the context of a PR but I think that on a push to the default branch, we should check the entire codebase. Otherwise, one cannot really check if their PR is OK before pushing the changes because golangci-lint would always fail locally due to a huge number of pre-existing failures.

If you don't mind, I'll try to fix the errors reported by the errcheck in the entire codebase then and then I'll reenable the check here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind, I'll try to fix the errors reported by the errcheck in the entire codebase then and then I'll reenable the check here.

That works too. Thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant revive, of course, not errcheck, as errcheck is already disabled on the master branch 🤦 So I fixed all the issues reported by revive now 🥳 This should be ready for a re-review. Thank you for the feedback 🙇


severity:
default: warning

default: warning
4 changes: 2 additions & 2 deletions core/record/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func TestEnvelopeHappyPath(t *testing.T) {

func TestConsumeTypedEnvelope(t *testing.T) {
var (
rec = simpleRecord{message: "hello world!"}
priv, _, err = test.RandTestKeyPair(crypto.Ed25519, 256)
rec = simpleRecord{message: "hello world!"}
priv, _, _ = test.RandTestKeyPair(crypto.Ed25519, 256)
)

envelope, err := Seal(&rec, priv)
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func makeHost(port int, randomness io.Reader) (host.Host, error) {
)
}

func startPeer(ctx context.Context, h host.Host, streamHandler network.StreamHandler) {
func startPeer(_ context.Context, h host.Host, streamHandler network.StreamHandler) {
// Set a function as stream handler.
// This function is called when a peer connects, and starts a stream with this protocol.
// Only applies on the receiving side.
Expand All @@ -193,7 +193,7 @@ func startPeer(ctx context.Context, h host.Host, streamHandler network.StreamHan
log.Println()
}

func startPeerAndConnect(ctx context.Context, h host.Host, destination string) (*bufio.ReadWriter, error) {
func startPeerAndConnect(_ context.Context, h host.Host, destination string) (*bufio.ReadWriter, error) {
log.Println("This node's multiaddresses:")
for _, la := range h.Addrs() {
log.Printf(" - %v\n", la)
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func getHostAddress(ha host.Host) string {
return addr.Encapsulate(hostAddr).String()
}

func startListener(ctx context.Context, ha host.Host, listenPort int, insecure bool) {
func startListener(_ context.Context, ha host.Host, listenPort int, insecure bool) {
fullAddr := getHostAddress(ha)
log.Printf("I am %s\n", fullAddr)

Expand All @@ -121,7 +121,7 @@ func startListener(ctx context.Context, ha host.Host, listenPort int, insecure b
}
}

func runSender(ctx context.Context, ha host.Host, targetPeer string) {
func runSender(_ context.Context, ha host.Host, targetPeer string) {
fullAddr := getHostAddress(ha)
log.Printf("I am %s\n", fullAddr)

Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-camp-2019/07-Messaging/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func pubsubMessageHandler(id peer.ID, msg *SendMessage) {
fmt.Printf("%s: %s\n", id.ShortString(), msg.Data)
}

func pubsubUpdateHandler(id peer.ID, msg *UpdatePeer) {
func pubsubUpdateHandler(_ peer.ID, _ *UpdatePeer) {

}

Expand Down
2 changes: 0 additions & 2 deletions p2p/discovery/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func TestFixedBackoff(t *testing.T) {
delay := startDelay

bkf := NewFixedBackoff(delay)
delay *= 2
b1 := bkf()
delay *= 2
b2 := bkf()

if b1.Delay() != startDelay || b2.Delay() != startDelay {
Expand Down
1 change: 0 additions & 1 deletion p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ func (as *AmbientAutoNAT) scheduleProbe(forceProbe bool) time.Duration {
// retry very quicky if forceProbe is true *and* we don't know our reachability
// limit all peers fetch from peerstore to 1 per second.
nextProbeAfter = 2 * time.Second
nextProbeAfter = 2 * time.Second
case currentStatus == network.ReachabilityUnknown,
as.confidence < maxConfidence,
currentStatus != network.ReachabilityPublic && receivedInbound:
Expand Down
2 changes: 1 addition & 1 deletion p2p/http/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func ExampleHost_overLibp2pStreams() {
// Make an HTTP request using the Go standard library, but over libp2p
// streams. If the server were listening on an HTTP transport, this could
// also make the request over the HTTP transport.
httpClient, err := client.NamespacedClient("/echo/1.0.0", peer.AddrInfo{ID: server.PeerID(), Addrs: server.Addrs()})
httpClient, _ := client.NamespacedClient("/echo/1.0.0", peer.AddrInfo{ID: server.PeerID(), Addrs: server.Addrs()})

// Only need to Post to "/" because this client is namespaced to the "/echo/1.0.0" protocol.
resp, err := httpClient.Post("/", "application/octet-stream", strings.NewReader("Hello HTTP"))
Expand Down
4 changes: 2 additions & 2 deletions p2p/net/connmgr/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func TestPeerProtectionSingleTag(t *testing.T) {
}

// protect the first 5 peers.
var protected []network.Conn
protected := make([]network.Conn, 0, 5)
for _, c := range conns[0:5] {
cm.Protect(c.RemotePeer(), "global")
protected = append(protected, c)
Expand Down Expand Up @@ -610,7 +610,7 @@ func TestPeerProtectionMultipleTags(t *testing.T) {
}

// protect the first 5 peers under two tags.
var protected []network.Conn
protected := make([]network.Conn, 0, 5)
for _, c := range conns[0:5] {
cm.Protect(c.RemotePeer(), "tag1")
cm.Protect(c.RemotePeer(), "tag2")
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/nat/internal/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (e ErrNoNATFound) Unwrap() []error {
}

func (e ErrNoNATFound) Error() string {
var errStrs []string
errStrs := make([]string, 0, len(e.Errs))
for _, err := range e.Errs {
errStrs = append(errStrs, err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions p2p/net/swarm/black_hole_detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
func TestBlackHoleSuccessCounterReset(t *testing.T) {
n := 10
bhf := &BlackHoleSuccessCounter{N: n, MinSuccesses: 2, Name: "test"}
var i = 0
// calls up to n should be probing
for i = 1; i <= n; i++ {
for i := 1; i <= n; i++ {
if bhf.HandleRequest() != blackHoleStateProbing {
t.Fatalf("expected calls up to n to be probes")
}
Expand All @@ -24,7 +23,7 @@ func TestBlackHoleSuccessCounterReset(t *testing.T) {
}

// after threshold calls every nth call should be a probe
for i = n + 1; i < 42; i++ {
for i := n + 1; i < 42; i++ {
result := bhf.HandleRequest()
if (i%n == 0 && result != blackHoleStateProbing) || (i%n != 0 && result != blackHoleStateBlocked) {
t.Fatalf("expected every nth dial to be a probe")
Expand All @@ -36,7 +35,7 @@ func TestBlackHoleSuccessCounterReset(t *testing.T) {

bhf.RecordResult(true)
// check if calls up to n are probes again
for i = 0; i < n; i++ {
for i := 0; i < n; i++ {
if bhf.HandleRequest() != blackHoleStateProbing {
t.Fatalf("expected black hole detector state to reset after success")
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func makeReservationMsg(
return rsvp
}

var addrBytes [][]byte
addrBytes := make([][]byte, 0, len(selfAddrs))
for _, addr := range selfAddrs {
if !manet.IsPublicAddr(addr) {
continue
Expand Down
2 changes: 1 addition & 1 deletion p2p/protocol/circuitv2/relay/relay_priv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestMakeReservationWithP2PAddrs(t *testing.T) {
"/ip4/1.2.3.4/tcp/1235/p2p/" + selfID.String(),
}

var addrsFromRsvp []string
addrsFromRsvp := make([]string, 0, len(rsvp.GetAddrs()))
for _, addr := range rsvp.GetAddrs() {
a, err := ma.NewMultiaddrBytes(addr)
require.NoError(t, err)
Expand Down