Prechádzať zdrojové kódy

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
Alex Crawford 4 rokov pred
rodič
commit
1cbc38d0ea

+ 5 - 5
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()

+ 1 - 3
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];

+ 1 - 1
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))

+ 1 - 1
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)
     );

+ 1 - 1
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();
             }

+ 2 - 2
examples/utils.rs

@@ -42,7 +42,7 @@ pub fn setup_logging_with_clock<F>(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 })
             }