Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: activate clippy::manual_let_else lint #4770

Merged
merged 21 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions core/src/transport/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@
let mut hub = self.0.lock();

let port = if let Some(port) = NonZeroU64::new(port) {
port

Check failure on line 62 in core/src/transport/memory.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/core/src/transport/memory.rs
} else {
loop {
let port = match NonZeroU64::new(rand::random()) {
Some(p) => p,
None => continue,
};
let Some(port) = NonZeroU64::new(rand::random()) else { continue };
if !hub.contains_key(&port) {
break port;
}
Expand Down Expand Up @@ -184,15 +181,12 @@
id: ListenerId,
addr: Multiaddr,
) -> Result<(), TransportError<Self::Error>> {
let port = if let Ok(port) = parse_memory_addr(&addr) {
port
} else {
let Ok(port) = parse_memory_addr(&addr) else {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
return Err(TransportError::MultiaddrNotSupported(addr));

Check failure on line 185 in core/src/transport/memory.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/core/src/transport/memory.rs
};

let (rx, port) = match HUB.register_port(port) {
Some((rx, port)) => (rx, port),
None => return Err(TransportError::Other(MemoryTransportError::Unreachable)),
let Some((rx, port)) = HUB.register_port(port) else {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
return Err(TransportError::Other(MemoryTransportError::Unreachable))
};

let listener = Listener {
Expand Down
4 changes: 1 addition & 3 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ impl Behaviour {
..
}: DialFailure,
) {
let peer_id = if let Some(peer_id) = peer_id {
peer_id
} else {
let Some(peer_id) = peer_id else {
eserilev marked this conversation as resolved.
Show resolved Hide resolved
return;
};

Expand Down
30 changes: 12 additions & 18 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1884,15 +1884,12 @@ where

let mut unsubscribed_peers = Vec::new();

let subscribed_topics = match self.peer_topics.get_mut(propagation_source) {
Some(topics) => topics,
None => {
error!(
"Subscription by unknown peer: {}",
propagation_source.to_string()
);
return;
}
let Some(subscribed_topics) = self.peer_topics.get_mut(propagation_source) else {
error!(
"Subscription by unknown peer: {}",
propagation_source.to_string()
);
return;
};

// Collect potential graft topics for the peer.
Expand Down Expand Up @@ -3139,15 +3136,12 @@ where
// remove from mesh, topic_peers, peer_topic and the fanout
debug!("Peer disconnected: {}", peer_id);
{
let topics = match self.peer_topics.get(&peer_id) {
Some(topics) => topics,
None => {
debug_assert!(
self.blacklisted_peers.contains(&peer_id),
"Disconnected node not in connected list"
);
return;
}
let Some(topics) = self.peer_topics.get(&peer_id) else {
debug_assert!(
self.blacklisted_peers.contains(&peer_id),
"Disconnected node not in connected list"
);
return;
};

// remove peer from all mappings
Expand Down
6 changes: 1 addition & 5 deletions protocols/gossipsub/src/peer_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,10 @@
self.metric_score(peer_id, None)
}

/// Returns the score for a peer, logging metrics. This is called from the heartbeat and

Check failure on line 221 in protocols/gossipsub/src/peer_score.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/protocols/gossipsub/src/peer_score.rs
/// increments the metric counts for penalties.
pub(crate) fn metric_score(&self, peer_id: &PeerId, mut metrics: Option<&mut Metrics>) -> f64 {
let peer_stats = match self.peer_stats.get(peer_id) {
Some(v) => v,
None => return 0.0,
};

let Some(peer_stats) = self.peer_stats.get(peer_id) else { return 0.0 };
let mut score = 0.0;

// topic scores
Expand Down
33 changes: 10 additions & 23 deletions protocols/gossipsub/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,19 @@
fn verify_signature(message: &proto::Message) -> bool {
use quick_protobuf::MessageWrite;

let from = match message.from.as_ref() {
Some(v) => v,
None => {
debug!("Signature verification failed: No source id given");
return false;
}
let Some(from) = message.from.as_ref() else {
debug!("Signature verification failed: No source id given");
return false;
};

let source = match PeerId::from_bytes(from) {
Ok(v) => v,
Err(_) => {
debug!("Signature verification failed: Invalid Peer Id");
return false;
}
let Ok(source) = PeerId::from_bytes(from) else {
debug!("Signature verification failed: Invalid Peer Id");
return false;
};

let signature = match message.signature.as_ref() {
Some(v) => v,
None => {
debug!("Signature verification failed: No signature provided");
return false;
}
let Some(signature) = message.signature.as_ref() else {
debug!("Signature verification failed: No signature provided");
return false;
};

// If there is a key value in the protobuf, use that key otherwise the key must be
Expand Down Expand Up @@ -235,14 +226,10 @@

impl Decoder for GossipsubCodec {
type Item = HandlerEvent;
type Error = quick_protobuf_codec::Error;

Check failure on line 229 in protocols/gossipsub/src/protocol.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/protocols/gossipsub/src/protocol.rs

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let rpc = match self.codec.decode(src)? {
Some(p) => p,
None => return Ok(None),
};

let Some(rpc) = self.codec.decode(src)? else { return Ok(None) };
// Store valid messages.
let mut messages = Vec::with_capacity(rpc.publish.len());
// Store any invalid messages.
Expand Down
6 changes: 1 addition & 5 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,11 +1965,7 @@ where
}

fn on_dial_failure(&mut self, DialFailure { peer_id, error, .. }: DialFailure) {
let peer_id = match peer_id {
Some(id) => id,
// Not interested in dial failures to unknown peers.
None => return,
};
let Some(peer_id) = peer_id else { return };

match error {
DialError::LocalPeerId { .. }
Expand Down
5 changes: 1 addition & 4 deletions protocols/kad/src/query/peers/closest/disjoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ impl<I: Iterator<Item = Key<PeerId>>> Iterator for ResultIter<I> {
.iter_mut()
// Find the iterator with the next closest peer.
.fold(Option::<&mut Peekable<_>>::None, |iter_a, iter_b| {
let iter_a = match iter_a {
Some(iter_a) => iter_a,
None => return Some(iter_b),
};
let Some(iter_a) = iter_a else { return Some(iter_b) };

match (iter_a.peek(), iter_b.peek()) {
(Some(next_a), Some(next_b)) => {
Expand Down
11 changes: 4 additions & 7 deletions protocols/relay/src/priv_client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,10 @@ impl Stream for Listener {
return Poll::Ready(None);
}

let msg = match ready!(self.from_behaviour.poll_next_unpin(cx)) {
Some(msg) => msg,
None => {
// Sender of `from_behaviour` has been dropped, signaling listener to close.
self.close(Ok(()));
continue;
}
let Some(msg) = ready!(self.from_behaviour.poll_next_unpin(cx)) else {
// Sender of `from_behaviour` has been dropped, signaling listener to close.
self.close(Ok(()));
continue;
};

match msg {
Expand Down
5 changes: 1 addition & 4 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1714,10 +1714,7 @@ impl NetworkInfo {
/// If the given address is not yet a `p2p` address for the given peer,
/// the `/p2p/<peer-id>` protocol is appended to the returned address.
fn p2p_addr(peer: Option<PeerId>, addr: Multiaddr) -> Result<Multiaddr, Multiaddr> {
let peer = match peer {
Some(p) => p,
None => return Ok(addr),
};
let Some(peer) = peer else { return Ok(addr) };

if let Some(multiaddr::Protocol::P2p(peer_id)) = addr.iter().last() {
if peer_id != peer {
Expand Down
5 changes: 1 addition & 4 deletions transports/noise/src/io/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ fn decrypt(
ciphertext: &mut BytesMut,
decrypt_fn: impl FnOnce(&[u8], &mut [u8]) -> Result<usize, snow::Error>,
) -> io::Result<Option<Bytes>> {
let ciphertext = match decode_length_prefixed(ciphertext)? {
Some(b) => b,
None => return Ok(None),
};
let Some(ciphertext) = decode_length_prefixed(ciphertext)? else { return Ok(None) };

log::trace!("Incoming ciphertext has {} bytes", ciphertext.len());

Expand Down
9 changes: 2 additions & 7 deletions transports/tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,7 @@ where
id: ListenerId,
addr: Multiaddr,
) -> Result<(), TransportError<Self::Error>> {
let socket_addr = if let Ok(sa) = multiaddr_to_socketaddr(addr.clone()) {
sa
} else {
let Ok(socket_addr) = multiaddr_to_socketaddr(addr.clone()) else {
eserilev marked this conversation as resolved.
Show resolved Hide resolved
return Err(TransportError::MultiaddrNotSupported(addr));
};
log::debug!("listening on {}", socket_addr);
Expand Down Expand Up @@ -664,10 +662,7 @@ where

/// Poll for a next If Event.
fn poll_if_addr(&mut self, cx: &mut Context<'_>) -> Poll<<Self as Stream>::Item> {
let if_watcher = match self.if_watcher.as_mut() {
Some(if_watcher) => if_watcher,
None => return Poll::Pending,
};
let Some(if_watcher) = self.if_watcher.as_mut() else { return Poll::Pending };

let my_listen_addr_port = self.listen_addr.port();

Expand Down
8 changes: 2 additions & 6 deletions transports/tls/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,9 @@ where
fn extract_single_certificate(
state: &CommonState,
) -> Result<P2pCertificate<'_>, certificate::ParseError> {
let cert = match state
let [cert] = state
.peer_certificates()
.expect("config enforces presence of certificates")
{
[single] => single,
_ => panic!("config enforces exactly one certificate"),
};
.expect("config enforces presence of certificates") else { panic!("config enforces exactly one certificate"); };
eserilev marked this conversation as resolved.
Show resolved Hide resolved

certificate::parse(cert)
}
Loading