From 42a491f8b1b7c7bb56a1e744850cc7f2ca8f0de2 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Thu, 7 Mar 2019 19:25:50 +0100 Subject: Clean up as suggested by clippy --- src/lib.rs | 23 ++++++++++++++--------- src/tftpc.rs | 32 +++++++++++++------------------- src/tftpd.rs | 32 ++++++++++++++++---------------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d5523a..669d9e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,13 +39,18 @@ fn default_options() -> TftpOptions { } } -impl Tftp { - - pub fn new() -> Tftp { +impl Default for Tftp { + fn default() -> Tftp { Tftp{ options: default_options(), } } +} + +impl Tftp { + pub fn new() -> Tftp { + Default::default() + } fn get_tftp_str(&self, buf: &[u8]) -> Option<(String, usize)> { let mut iter = buf.iter(); @@ -59,7 +64,7 @@ impl Tftp { Err(_) => return None, }; - return Some((val, len)); + Some((val, len)) } pub fn append_option(&self, buf: &mut Vec, key: &str, val: &str) { @@ -99,7 +104,7 @@ impl Tftp { _ => std::io::ErrorKind::InvalidData, }; - return std::io::Error::new(kind, error); + std::io::Error::new(kind, error) } fn wait_for_ack(&self, sock: &UdpSocket, expected_block: u16) -> Result { @@ -196,9 +201,9 @@ impl Tftp { } }); - sock.set_read_timeout(Some(Duration::from_secs(self.options.timeout as u64)))?; + sock.set_read_timeout(Some(Duration::from_secs(u64::from(self.options.timeout))))?; - return Ok(()); + Ok(()) } pub fn parse_options(&self, buf: &[u8]) -> HashMap { @@ -221,7 +226,7 @@ impl Tftp { options.insert(key, val); } - return options; + options } pub fn parse_file_mode_options(&self, buf: &[u8]) -> Result<(PathBuf, String, HashMap), io::Error> { @@ -349,7 +354,7 @@ impl Tftp { return Err(io::Error::new(io::ErrorKind::InvalidInput, "unexpected size")); } - let _opcode = match u16::from_be_bytes([buf[0], buf[1]]) { + match u16::from_be_bytes([buf[0], buf[1]]) { // opcode opc if opc == Opcodes::DATA as u16 => (), opc if opc == Opcodes::ERROR as u16 => return Err(self.parse_error(&buf[.. len])), _ => return Err(io::Error::new(io::ErrorKind::Other, "unexpected opcode")), diff --git a/src/tftpc.rs b/src/tftpc.rs index 6aa61c9..669b455 100644 --- a/src/tftpc.rs +++ b/src/tftpc.rs @@ -36,7 +36,7 @@ impl Tftpc { pub fn new(conf: Configuration) -> Tftpc { Tftpc { tftp: rtftp::Tftp::new(), - conf: conf, + conf, } } @@ -148,11 +148,11 @@ impl Tftpc { } match self.tftp.send_file(&sock, &mut file) { - Ok(_) => return Ok(format!("Sent {} to {}.", self.conf.filename.display(), self.conf.remote)), + Ok(_) => Ok(format!("Sent {} to {}.", self.conf.filename.display(), self.conf.remote)), Err(err) => { let error = format!("Sending {} to {} failed ({}).", self.conf.filename.display(), self.conf.remote, err); self.tftp.send_error(&sock, 0, "Sending error")?; - return Err(io::Error::new(err.kind(), error)); + Err(io::Error::new(err.kind(), error)) }, } } @@ -197,11 +197,11 @@ impl Tftpc { } match self.tftp.recv_file(&sock, &mut file) { - Ok(_) => return Ok(format!("Received {} from {}.", self.conf.filename.display(), self.conf.remote)), + Ok(_) => Ok(format!("Received {} from {}.", self.conf.filename.display(), self.conf.remote)), Err(err) => { let error = format!("Receiving {} from {} failed ({}).", self.conf.filename.display(), self.conf.remote, err); self.tftp.send_error(&sock, 0, "Receiving error")?; - return Err(std::io::Error::new(err.kind(), error)); + Err(std::io::Error::new(err.kind(), error)) }, } } @@ -232,7 +232,7 @@ fn usage(opts: Options, program: String, error: Option) { println!("{}", opts.usage(format!("RusTFTP\n\n{} [options] [:port]", program).as_str())); } -fn parse_commandline<'a>(args: &'a Vec) -> Result { +fn parse_commandline(args: &[String]) -> Result { let program = args[0].clone(); let mut mode = None; let mut filename = None; @@ -255,19 +255,13 @@ fn parse_commandline<'a>(args: &'a Vec) -> Result { - mode = Some(Mode::RRQ); - filename = Some(Path::new(&f).to_path_buf()); - } - None => () + if let Some(f) = matches.opt_str("g") { + mode = Some(Mode::RRQ); + filename = Some(Path::new(&f).to_path_buf()); } - match matches.opt_str("p") { - Some(f) => { - mode = Some(Mode::WRQ); - filename = Some(Path::new(&f).to_path_buf()); - } - None => () + if let Some(f) = matches.opt_str("p") { + mode = Some(Mode::WRQ); + filename = Some(Path::new(&f).to_path_buf()); } if mode.is_none() || (matches.opt_present("g") && matches.opt_present("p")) { @@ -301,7 +295,7 @@ fn parse_commandline<'a>(args: &'a Vec) -> Result Tftpd { Tftpd{ tftp: rtftp::Tftp::new(), - conf: conf, + conf, } } @@ -68,7 +68,7 @@ impl Tftpd { match path.strip_prefix(cwd) { Ok(p) => Some(p.to_path_buf()), - Err(_) => return None, + Err(_) => None, } } @@ -110,11 +110,11 @@ impl Tftpd { self.tftp.ack_options(&socket, &options, false)?; match self.tftp.recv_file(&socket, &mut file) { - Ok(_) => return Ok(format!("Received {} from {}.", path.display(), cl)), + Ok(_) => Ok(format!("Received {} from {}.", path.display(), cl)), Err(ref err) => { let error = format!("Receiving {} from {} failed ({}).", path.display(), cl, err); self.tftp.send_error(&socket, 0, "Receiving error")?; - return Err(io::Error::new(err.kind(), error)); + Err(io::Error::new(err.kind(), error)) } } } @@ -163,10 +163,10 @@ impl Tftpd { } self.tftp.ack_options(&socket, &options, true)?; match self.tftp.send_file(&socket, &mut file) { - Ok(_) => return Ok(format!("Sent {} to {}.", path.display(), cl)), + Ok(_) => Ok(format!("Sent {} to {}.", path.display(), cl)), Err(err) => { let error = format!("Sending {} to {} failed ({}).", path.display(), cl, err.to_string()); - return Err(std::io::Error::new(err.kind(), error)); + Err(std::io::Error::new(err.kind(), error)) } } } @@ -176,29 +176,29 @@ impl Tftpd { socket.set_read_timeout(Some(Duration::from_secs(5)))?; socket.connect(cl)?; - let _opcode = match u16::from_be_bytes([buf[0], buf[1]]) { + match u16::from_be_bytes([buf[0], buf[1]]) { // opcode o if o == rtftp::Opcodes::RRQ as u16 => { if self.conf.wo { self.tftp.send_error(&socket, 4, "reading not allowed")?; - return Err(io::Error::new(io::ErrorKind::Other, "unallowed mode")); + Err(io::Error::new(io::ErrorKind::Other, "unallowed mode")) } else { - return self.handle_rrq(&socket, &cl, &buf[2..]); + self.handle_rrq(&socket, &cl, &buf[2..]) } }, o if o == rtftp::Opcodes::WRQ as u16 => { if self.conf.ro { self.tftp.send_error(&socket, 4, "writing not allowed")?; - return Err(io::Error::new(io::ErrorKind::Other, "unallowed mode")); + Err(io::Error::new(io::ErrorKind::Other, "unallowed mode")) } else { - return self.handle_wrq(&socket, &cl, &buf[2..]); + self.handle_wrq(&socket, &cl, &buf[2..]) } }, - o if o == rtftp::Opcodes::ERROR as u16 => return Ok(format!("Received ERROR from {}", cl)), + o if o == rtftp::Opcodes::ERROR as u16 => Ok(format!("Received ERROR from {}", cl)), _ => { self.tftp.send_error(&socket, 4, "Unexpected opcode")?; - return Err(io::Error::new(io::ErrorKind::Other, "unexpected opcode")); + Err(io::Error::new(io::ErrorKind::Other, "unexpected opcode")) } - }; + } } fn drop_privs(&self, uid: u32, gid: u32) -> Result<(), Box> { @@ -275,7 +275,7 @@ fn usage(opts: Options, program: String, error: Option) { println!("{}", opts.usage(format!("RusTFTP\n\n{} [options]", program).as_str())); } -fn parse_commandline<'a>(args: &'a Vec) -> Result { +fn parse_commandline(args: &[String]) -> Result { let program = args[0].clone(); let mut conf = Configuration{ port: 69, @@ -342,7 +342,7 @@ fn parse_commandline<'a>(args: &'a Vec) -> Result