Skip to content

Commit

Permalink
chore: activate clippy::manual_let_else lint
Browse files Browse the repository at this point in the history
Refactor let else usage to align w/ idiomatic usage of Rust by activating `clippy::manual_let_else` lint, and resolving all the warning messages.

Resolves: libp2p#4741.

Pull-Request: libp2p#4770.
  • Loading branch information
eserilev authored Nov 14, 2023
1 parent 39a40bf commit eb341ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
17 changes: 5 additions & 12 deletions src/behaviour/as_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,9 @@ impl<'a> AsClient<'a> {
tracing::debug!("Outbound dial-back request aborted: No dial-back addresses");
return Err(OutboundProbeError::NoAddresses);
}
let server = match self.random_server() {
Some(s) => s,
None => {
tracing::debug!("Outbound dial-back request aborted: No qualified server");
return Err(OutboundProbeError::NoServer);
}
};

let server = self.random_server().ok_or(OutboundProbeError::NoServer)?;

let request_id = self.inner.send_request(
&server,
DialRequest {
Expand All @@ -301,11 +297,8 @@ impl<'a> AsClient<'a> {
// Set the delay to the next probe based on the time of our last probe
// and the specified delay.
fn schedule_next_probe(&mut self, delay: Duration) {
let last_probe_instant = match self.last_probe {
Some(instant) => instant,
None => {
return;
}
let Some(last_probe_instant) = self.last_probe else {
return;
};
let schedule_next = *last_probe_instant + delay;
self.schedule_probe
Expand Down
8 changes: 4 additions & 4 deletions src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,13 @@ impl<'a> AsServer<'a> {
demanded: Vec<Multiaddr>,
observed_remote_at: &Multiaddr,
) -> Vec<Multiaddr> {
let observed_ip = match observed_remote_at
let Some(observed_ip) = observed_remote_at
.into_iter()
.find(|p| matches!(p, Protocol::Ip4(_) | Protocol::Ip6(_)))
{
Some(ip) => ip,
None => return Vec::new(),
else {
return Vec::new();
};

let mut distinct = HashSet::new();
demanded
.into_iter()
Expand Down
28 changes: 12 additions & 16 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,18 @@ impl DialRequest {
if msg.type_pb != Some(proto::MessageType::DIAL) {
return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid type"));
}
let (peer_id, addrs) = if let Some(proto::Dial {
peer:
Some(proto::PeerInfo {
id: Some(peer_id),
addrs,
}),
}) = msg.dial
{
(peer_id, addrs)
} else {
tracing::debug!("Received malformed dial message");
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"invalid dial message",
));
};

let peer_id_result = msg.dial.and_then(|dial| {
dial.peer.and_then(|peer_info| {
let Some(peer_id) = peer_info.id else {
return None;
};
Some((peer_id, peer_info.addrs))
})
});

let (peer_id, addrs) = peer_id_result
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid dial message"))?;

let peer_id = {
PeerId::try_from(peer_id.to_vec())
Expand Down

0 comments on commit eb341ed

Please sign in to comment.