Skip to content

Commit

Permalink
feat(ci): lint against usages of variables with underscore
Browse files Browse the repository at this point in the history
Prefixing a variable with an underscore (`_`) in Rust indicates that it is not used. Through refactorings, it can sometimes happen that we do end up using such a variable. In this case, the underscore should be removed.

Clippy can help us with this.

Pull-Request: #3484.
  • Loading branch information
thomaseizinger authored Feb 24, 2023
1 parent 6383e1e commit d80d92d
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[alias]
# Temporary solution to have clippy config in a single place until https://github.com/rust-lang/rust-clippy/blob/master/doc/roadmap-2021.md#lintstoml-configuration is shipped.
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings"
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -W clippy::used_underscore_binding -D warnings"
4 changes: 2 additions & 2 deletions core/src/transport/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,14 +641,14 @@ mod tests {
};

let dialer = async move {
let _chan = MemoryTransport::default()
let chan = MemoryTransport::default()
.dial(listener_addr_cloned)
.unwrap()
.await
.unwrap();

should_terminate.await.unwrap();
drop(_chan);
drop(chan);
terminated.send(()).unwrap();
};

Expand Down
20 changes: 10 additions & 10 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,13 @@ impl NetworkBehaviour for Behaviour {

fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
self.inner.handle_established_inbound_connection(
_connection_id,
connection_id,
peer,
local_addr,
remote_addr,
Expand All @@ -512,28 +512,28 @@ impl NetworkBehaviour for Behaviour {

fn handle_pending_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
maybe_peer: Option<PeerId>,
_addresses: &[Multiaddr],
_effective_role: Endpoint,
addresses: &[Multiaddr],
effective_role: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
self.inner.handle_pending_outbound_connection(
_connection_id,
connection_id,
maybe_peer,
_addresses,
_effective_role,
addresses,
effective_role,
)
}

fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
self.inner
.handle_established_outbound_connection(_connection_id, peer, addr, role_override)
.handle_established_outbound_connection(connection_id, peer, addr, role_override)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
Expand Down
6 changes: 3 additions & 3 deletions protocols/autonat/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ async fn test_throttle_global_max() {
..Default::default()
}))
.await;
let mut _handles = Vec::new();
let mut handles = Vec::new();
for _ in 0..2 {
let (_handle, rx) = oneshot::channel();
let (handle, rx) = oneshot::channel();
spawn_client(true, false, server_id, server_addr.clone(), rx).await;
_handles.push(_handle);
handles.push(handle);
}

let (first_probe_id, first_peer_id) = match next_event(&mut server).await {
Expand Down
6 changes: 3 additions & 3 deletions protocols/gossipsub/src/peer_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ impl PeerScore {
}
}

pub fn validate_message(&mut self, _from: &PeerId, msg_id: &MessageId, topic_hash: &TopicHash) {
pub fn validate_message(&mut self, from: &PeerId, msg_id: &MessageId, topic_hash: &TopicHash) {
// adds an empty record with the message id
self.deliveries
.entry(msg_id.clone())
Expand All @@ -572,12 +572,12 @@ impl PeerScore {
if let Some(callback) = self.message_delivery_time_callback {
if self
.peer_stats
.get(_from)
.get(from)
.and_then(|s| s.topics.get(topic_hash))
.map(|ts| ts.in_mesh())
.unwrap_or(false)
{
callback(_from, topic_hash, 0.0);
callback(from, topic_hash, 0.0);
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions swarm/src/behaviour/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ where

fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
let handler = match self {
Either::Left(inner) => Either::Left(inner.handle_established_inbound_connection(
_connection_id,
connection_id,
peer,
local_addr,
remote_addr,
)?),
Either::Right(inner) => Either::Right(inner.handle_established_inbound_connection(
_connection_id,
connection_id,
peer,
local_addr,
remote_addr,
Expand All @@ -73,23 +73,23 @@ where

fn handle_pending_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
maybe_peer: Option<PeerId>,
_addresses: &[Multiaddr],
_effective_role: Endpoint,
addresses: &[Multiaddr],
effective_role: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
let addresses = match self {
Either::Left(inner) => inner.handle_pending_outbound_connection(
_connection_id,
connection_id,
maybe_peer,
_addresses,
_effective_role,
addresses,
effective_role,
)?,
Either::Right(inner) => inner.handle_pending_outbound_connection(
_connection_id,
connection_id,
maybe_peer,
_addresses,
_effective_role,
addresses,
effective_role,
)?,
};

Expand All @@ -98,20 +98,20 @@ where

fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
let handler = match self {
Either::Left(inner) => Either::Left(inner.handle_established_outbound_connection(
_connection_id,
connection_id,
peer,
addr,
role_override,
)?),
Either::Right(inner) => Either::Right(inner.handle_established_outbound_connection(
_connection_id,
connection_id,
peer,
addr,
role_override,
Expand Down
20 changes: 10 additions & 10 deletions swarm/src/behaviour/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where

fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
Expand All @@ -101,7 +101,7 @@ where
};

let handler = inner.handle_established_inbound_connection(
_connection_id,
connection_id,
peer,
local_addr,
remote_addr,
Expand All @@ -114,29 +114,29 @@ where

fn handle_pending_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
maybe_peer: Option<PeerId>,
_addresses: &[Multiaddr],
_effective_role: Endpoint,
addresses: &[Multiaddr],
effective_role: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
let inner = match self.inner.as_mut() {
None => return Ok(vec![]),
Some(inner) => inner,
};

let addresses = inner.handle_pending_outbound_connection(
_connection_id,
connection_id,
maybe_peer,
_addresses,
_effective_role,
addresses,
effective_role,
)?;

Ok(addresses)
}

fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
Expand All @@ -147,7 +147,7 @@ where
};

let handler = inner.handle_established_outbound_connection(
_connection_id,
connection_id,
peer,
addr,
role_override,
Expand Down
2 changes: 1 addition & 1 deletion swarm/src/handler/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl ConnectionHandler for PendingConnectionHandler {
info: _info,
}) => {
void::unreachable(protocol);
#[allow(unreachable_code)]
#[allow(unreachable_code, clippy::used_underscore_binding)]
{
void::unreachable(_info);
}
Expand Down
30 changes: 15 additions & 15 deletions swarm/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,19 @@ where

fn handle_established_inbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
self.handle_established_inbound_connection.push((
peer,
_connection_id,
connection_id,
local_addr.clone(),
remote_addr.clone(),
));
self.inner.handle_established_inbound_connection(
_connection_id,
connection_id,
peer,
local_addr,
remote_addr,
Expand All @@ -441,28 +441,28 @@ where

fn handle_pending_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
maybe_peer: Option<PeerId>,
_addresses: &[Multiaddr],
_effective_role: Endpoint,
addresses: &[Multiaddr],
effective_role: Endpoint,
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
self.handle_pending_outbound_connection.push((
maybe_peer,
_addresses.to_vec(),
_effective_role,
_connection_id,
addresses.to_vec(),
effective_role,
connection_id,
));
self.inner.handle_pending_outbound_connection(
_connection_id,
connection_id,
maybe_peer,
_addresses,
_effective_role,
addresses,
effective_role,
)
}

fn handle_established_outbound_connection(
&mut self,
_connection_id: ConnectionId,
connection_id: ConnectionId,
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
Expand All @@ -471,10 +471,10 @@ where
peer,
addr.clone(),
role_override,
_connection_id,
connection_id,
));
self.inner
.handle_established_outbound_connection(_connection_id, peer, addr, role_override)
.handle_established_outbound_connection(connection_id, peer, addr, role_override)
}

fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) {
Expand Down
Loading

0 comments on commit d80d92d

Please sign in to comment.