From ea4579d68aa36b72200936a19a953c9c89e0e6a1 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Tue, 29 Dec 2020 18:40:28 -0800 Subject: [PATCH] Clean up examples These were flagged by `cargo clippy`: warning: you seem to be trying to use match for destructuring a single pattern. Consider using `if let` warning: called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent warning: using `write!()` with a format string that ends in a single newline warning: useless conversion to the same type: `smoltcp::wire::Ipv4Address` warning: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()` warning: returning the result of a `let` binding from a block warning: use of `unwrap_or` followed by a function call --- examples/dhcp_client.rs | 10 +++++----- examples/loopback.rs | 4 +--- examples/multicast.rs | 2 +- examples/ping.rs | 2 +- examples/server.rs | 2 +- examples/utils.rs | 4 ++-- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/examples/dhcp_client.rs b/examples/dhcp_client.rs index daabc9c..7fa009c 100644 --- a/examples/dhcp_client.rs +++ b/examples/dhcp_client.rs @@ -1,3 +1,4 @@ +#![allow(clippy::option_map_unit_fn)] mod utils; use std::collections::BTreeMap; @@ -57,10 +58,10 @@ fn main() { }); config.map(|config| { println!("DHCP config: {:?}", config); - match config.address { - Some(cidr) => if cidr != prev_cidr { + if let Some(cidr) = config.address { + if cidr != prev_cidr { iface.update_ip_addrs(|addrs| { - addrs.iter_mut().nth(0) + addrs.iter_mut().next() .map(|addr| { *addr = IpCidr::Ipv4(cidr); }); @@ -68,11 +69,10 @@ fn main() { prev_cidr = cidr; println!("Assigned a new IPv4 address: {}", cidr); } - _ => {} } config.router.map(|router| iface.routes_mut() - .add_default_ipv4_route(router.into()) + .add_default_ipv4_route(router) .unwrap() ); iface.routes_mut() diff --git a/examples/loopback.rs b/examples/loopback.rs index 9d0c39b..c84f286 100644 --- a/examples/loopback.rs +++ b/examples/loopback.rs @@ -76,9 +76,7 @@ fn main() { utils::add_middleware_options(&mut opts, &mut free); let mut matches = utils::parse_options(&opts, free); - let device = utils::parse_middleware_options(&mut matches, device, /*loopback=*/true); - - device + utils::parse_middleware_options(&mut matches, device, /*loopback=*/true) }; let mut neighbor_cache_entries = [None; 8]; diff --git a/examples/multicast.rs b/examples/multicast.rs index 92f1876..e637467 100644 --- a/examples/multicast.rs +++ b/examples/multicast.rs @@ -83,7 +83,7 @@ fn main() { // For display purposes only - normally we wouldn't process incoming IGMP packets // in the application layer socket.recv() - .and_then(|payload| Ipv4Packet::new_checked(payload)) + .and_then(Ipv4Packet::new_checked) .and_then(|ipv4_packet| IgmpPacket::new_checked(ipv4_packet.payload())) .and_then(|igmp_packet| IgmpRepr::parse(&igmp_packet)) .map(|igmp_repr| println!("IGMP packet: {:?}", igmp_repr)) diff --git a/examples/ping.rs b/examples/ping.rs index 411e450..1d290d4 100644 --- a/examples/ping.rs +++ b/examples/ping.rs @@ -74,7 +74,7 @@ fn main() { let count = matches.opt_str("count").map(|s| usize::from_str(&s).unwrap()).unwrap_or(4); let interval = matches.opt_str("interval") .map(|s| Duration::from_secs(u64::from_str(&s).unwrap())) - .unwrap_or(Duration::from_secs(1)); + .unwrap_or_else(|| Duration::from_secs(1)); let timeout = Duration::from_secs( matches.opt_str("timeout").map(|s| u64::from_str(&s).unwrap()).unwrap_or(5) ); diff --git a/examples/server.rs b/examples/server.rs index 572c84a..95675c7 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -109,7 +109,7 @@ fn main() { if socket.can_send() { debug!("tcp:6969 send greeting"); - write!(socket, "hello\n").unwrap(); + writeln!(socket, "hello").unwrap(); debug!("tcp:6969 close"); socket.close(); } diff --git a/examples/utils.rs b/examples/utils.rs index 09d43d4..02b2de0 100644 --- a/examples/utils.rs +++ b/examples/utils.rs @@ -42,7 +42,7 @@ pub fn setup_logging_with_clock(filter: &str, since_startup: F) }) .filter(None, LevelFilter::Trace) .parse(filter) - .parse(&env::var("RUST_LOG").unwrap_or("".to_owned())) + .parse(&env::var("RUST_LOG").unwrap_or_else(|_| "".to_owned())) .init(); } @@ -68,7 +68,7 @@ pub fn parse_options(options: &Options, free: Vec<&str>) -> Matches { Ok(matches) => { if matches.opt_present("h") || matches.free.len() != free.len() { let brief = format!("Usage: {} [OPTION]... {}", - env::args().nth(0).unwrap(), free.join(" ")); + env::args().next().unwrap(), free.join(" ")); print!("{}", options.usage(&brief)); process::exit(if matches.free.len() != free.len() { 1 } else { 0 }) }