Prefer if-let syntax over single-pattern match

These were flagged by `cargo clippy`:

    warning: you seem to be trying to use match for destructuring a
             single pattern. Consider using `if let`

This also silences a few cases where the match couldn't be replaced with
an if because of the following error:

    error: attributes are not yet allowed on `if` expressions

Once we increase the minimum Rust version to 1.43, these can be updated
as well.
This commit is contained in:
Alex Crawford 2020-12-25 23:35:44 -08:00
parent b2c04416c2
commit 321998eb66
5 changed files with 20 additions and 32 deletions

View File

@ -1578,13 +1578,8 @@ impl<'b, 'c, 'e> InterfaceInner<'b, 'c, 'e> {
IpAddress::__Nonexhaustive =>
unreachable!()
};
match hardware_addr {
Some(hardware_addr) =>
// Destination is multicast
return Ok((hardware_addr, tx_token)),
None =>
// Continue
(),
if let Some(hardware_addr) = hardware_addr {
return Ok((hardware_addr, tx_token))
}
}

View File

@ -169,13 +169,11 @@ impl<'a> Cache<'a> {
return Answer::Found(EthernetAddress::BROADCAST);
}
match self.storage.get(protocol_addr) {
Some(&Neighbor { expires_at, hardware_addr }) => {
if timestamp < expires_at {
return Answer::Found(hardware_addr)
}
if let Some(&Neighbor { expires_at, hardware_addr }) =
self.storage.get(protocol_addr) {
if timestamp < expires_at {
return Answer::Found(hardware_addr)
}
None => ()
}
if timestamp < self.silent_until {

View File

@ -287,12 +287,14 @@ impl<'a> Parser<'a> {
fn accept_ip(&mut self) -> Result<IpAddress> {
#[cfg(feature = "proto-ipv4")]
#[allow(clippy::single_match)]
match self.try_do(|p| p.accept_ipv4()) {
Some(ipv4) => return Ok(IpAddress::Ipv4(ipv4)),
None => ()
}
#[cfg(feature = "proto-ipv6")]
#[allow(clippy::single_match)]
match self.try_do(|p| p.accept_ipv6(false)) {
Some(ipv6) => return Ok(IpAddress::Ipv6(ipv6)),
None => ()
@ -333,12 +335,14 @@ impl<'a> Parser<'a> {
fn accept_ip_endpoint(&mut self) -> Result<IpEndpoint> {
#[cfg(feature = "proto-ipv4")]
#[allow(clippy::single_match)]
match self.try_do(|p| p.accept_ipv4_endpoint()) {
Some(ipv4) => return Ok(ipv4),
None => ()
}
#[cfg(feature = "proto-ipv6")]
#[allow(clippy::single_match)]
match self.try_do(|p| p.accept_ipv6_endpoint()) {
Some(ipv6) => return Ok(ipv6),
None => ()
@ -424,12 +428,14 @@ impl FromStr for IpCidr {
/// Parse a string representation of an IP CIDR.
fn from_str(s: &str) -> Result<IpCidr> {
#[cfg(feature = "proto-ipv4")]
#[allow(clippy::single_match)]
match Ipv4Cidr::from_str(s) {
Ok(cidr) => return Ok(IpCidr::Ipv4(cidr)),
Err(_) => ()
}
#[cfg(feature = "proto-ipv6")]
#[allow(clippy::single_match)]
match Ipv6Cidr::from_str(s) {
Ok(cidr) => return Ok(IpCidr::Ipv6(cidr)),
Err(_) => ()

View File

@ -127,21 +127,16 @@ impl Timer {
}
fn set_keep_alive(&mut self) {
match *self {
Timer::Idle { ref mut keep_alive_at }
if keep_alive_at.is_none() => {
if let Timer::Idle { ref mut keep_alive_at } = *self {
if keep_alive_at.is_none() {
*keep_alive_at = Some(Instant::from_millis(0))
}
_ => ()
}
}
fn rewind_keep_alive(&mut self, timestamp: Instant, interval: Option<Duration>) {
match self {
&mut Timer::Idle { ref mut keep_alive_at } => {
*keep_alive_at = interval.map(|interval| timestamp + interval)
}
_ => ()
if let Timer::Idle { ref mut keep_alive_at } = *self {
*keep_alive_at = interval.map(|interval| timestamp + interval)
}
}

View File

@ -296,11 +296,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
let mut options = &data[field::OPTIONS(self.header_len())];
while !options.is_empty() {
let (next_options, option) = TcpOption::parse(options)?;
match option {
TcpOption::SackPermitted => {
return Ok(true);
},
_ => {},
if option == TcpOption::SackPermitted {
return Ok(true);
}
options = next_options;
}
@ -317,11 +314,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
let mut options = &data[field::OPTIONS(self.header_len())];
while !options.is_empty() {
let (next_options, option) = TcpOption::parse(options)?;
match option {
TcpOption::SackRange(slice) => {
return Ok(slice);
},
_ => {},
if let TcpOption::SackRange(slice) = option {
return Ok(slice);
}
options = next_options;
}