From 08eb56f12020179b1b4ee6c5b0af88969bf0912c Mon Sep 17 00:00:00 2001 From: John Eckersberg Date: Tue, 9 Mar 2021 16:23:48 -0500 Subject: [PATCH] Fix some clippy warnings --- src/uu/cp/src/cp.rs | 14 +++++------ src/uu/csplit/src/csplit.rs | 4 ++-- src/uu/ln/src/ln.rs | 13 ++++------ src/uu/mktemp/src/mktemp.rs | 24 ++++++++----------- src/uu/nl/src/helper.rs | 4 +++- src/uu/nl/src/nl.rs | 2 +- src/uu/od/src/multifilereader.rs | 2 +- src/uu/od/src/od.rs | 6 ++--- .../formatters/cninetyninehexfloatf.rs | 11 +++++---- .../tokenize/num_format/formatters/intf.rs | 7 +++--- src/uu/realpath/src/realpath.rs | 2 +- src/uu/shuf/src/shuf.rs | 4 ++-- src/uu/split/src/platform/unix.rs | 13 +++++----- src/uu/split/src/split.rs | 2 +- src/uu/tail/src/tail.rs | 6 ++++- src/uu/truncate/src/truncate.rs | 9 ++----- 16 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 5ae06c298..a43f3337c 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1238,15 +1238,13 @@ fn copy_helper(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> dest.into() }; symlink_file(&link, &dest, &*context_for(&link, &dest))?; + } else if source.to_string_lossy() == "/dev/null" { + /* workaround a limitation of fs::copy + * https://github.com/rust-lang/rust/issues/79390 + */ + File::create(dest)?; } else { - if source.to_string_lossy() == "/dev/null" { - /* workaround a limitation of fs::copy - * https://github.com/rust-lang/rust/issues/79390 - */ - File::create(dest)?; - } else { - fs::copy(source, dest).context(&*context_for(source, dest))?; - } + fs::copy(source, dest).context(&*context_for(source, dest))?; } Ok(()) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 0d2fc8121..7eb287522 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -478,7 +478,7 @@ where /// Shrink the buffer so that its length is equal to the set size, returning an iterator for /// the elements that were too much. - fn shrink_buffer_to_size<'a>(&'a mut self) -> impl Iterator + 'a { + fn shrink_buffer_to_size(&mut self) -> impl Iterator + '_ { let mut shrink_offset = 0; if self.buffer.len() > self.size { shrink_offset = self.buffer.len() - self.size; @@ -489,7 +489,7 @@ where } /// Drain the content of the buffer. - fn drain_buffer<'a>(&'a mut self) -> impl Iterator + 'a { + fn drain_buffer(&mut self) -> impl Iterator + '_ { self.buffer.drain(..).map(|(_, line)| line.unwrap()) } diff --git a/src/uu/ln/src/ln.rs b/src/uu/ln/src/ln.rs index e06468367..ab07a2b08 100644 --- a/src/uu/ln/src/ln.rs +++ b/src/uu/ln/src/ln.rs @@ -207,7 +207,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let paths: Vec = matches .values_of(ARG_FILES) .unwrap() - .map(|path| PathBuf::from(path)) + .map(PathBuf::from) .collect(); let overwrite_mode = if matches.is_present(OPT_FORCE) { @@ -316,9 +316,8 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting // We need to clean the target if is_symlink(target_dir) { if target_dir.is_file() { - match fs::remove_file(target_dir) { - Err(e) => show_error!("Could not update {}: {}", target_dir.display(), e), - _ => (), + if let Err(e) = fs::remove_file(target_dir) { + show_error!("Could not update {}: {}", target_dir.display(), e) }; } if target_dir.is_dir() { @@ -423,10 +422,8 @@ fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> { } } - if settings.no_dereference && settings.force { - if dst.exists() { - fs::remove_file(dst)?; - } + if settings.no_dereference && settings.force && dst.exists() { + fs::remove_file(dst)?; } if settings.symbolic { diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 663f7d4ad..194b6625f 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -120,13 +120,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // See https://github.com/clap-rs/clap/pull/1587 let tmp = env::temp_dir(); (tmpdir, tmp) + } else if !matches.is_present(OPT_TMPDIR) { + let tmp = env::temp_dir(); + (template, tmp) } else { - if !matches.is_present(OPT_TMPDIR) { - let tmp = env::temp_dir(); - (template, tmp) - } else { - (template, PathBuf::from(tmpdir)) - } + (template, PathBuf::from(tmpdir)) }; let make_dir = matches.is_present(OPT_DIRECTORY); @@ -158,14 +156,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { crash!(1, "suffix cannot contain any path separators"); } - if matches.is_present(OPT_TMPDIR) { - if PathBuf::from(prefix).is_absolute() { - show_info!( - "invalid template, ‘{}’; with --tmpdir, it may not be absolute", - template - ); - return 1; - } + if matches.is_present(OPT_TMPDIR) && PathBuf::from(prefix).is_absolute() { + show_info!( + "invalid template, ‘{}’; with --tmpdir, it may not be absolute", + template + ); + return 1; }; if matches.is_present(OPT_T) { diff --git a/src/uu/nl/src/helper.rs b/src/uu/nl/src/helper.rs index e34cfd2ae..9b98129f1 100644 --- a/src/uu/nl/src/helper.rs +++ b/src/uu/nl/src/helper.rs @@ -11,7 +11,9 @@ fn parse_style(chars: &[char]) -> Result { } else if chars.len() > 1 && chars[0] == 'p' { let s: String = chars[1..].iter().cloned().collect(); match regex::Regex::new(&s) { - Ok(re) => Ok(crate::NumberingStyle::NumberForRegularExpression(re)), + Ok(re) => Ok(crate::NumberingStyle::NumberForRegularExpression(Box::new( + re, + ))), Err(_) => Err(String::from("Illegal regular expression")), } } else { diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index 105bb196b..47b6c3ae9 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -55,7 +55,7 @@ enum NumberingStyle { NumberForAll, NumberForNonEmpty, NumberForNone, - NumberForRegularExpression(regex::Regex), + NumberForRegularExpression(Box), } // NumberFormat specifies how line numbers are output within their allocated diff --git a/src/uu/od/src/multifilereader.rs b/src/uu/od/src/multifilereader.rs index e41c9e39b..1255da66d 100644 --- a/src/uu/od/src/multifilereader.rs +++ b/src/uu/od/src/multifilereader.rs @@ -24,7 +24,7 @@ pub trait HasError { } impl<'b> MultifileReader<'b> { - pub fn new<'a>(fnames: Vec>) -> MultifileReader<'a> { + pub fn new(fnames: Vec) -> MultifileReader { let mut mf = MultifileReader { ni: fnames, curr_file: None, // normally this means done; call next_file() diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index dbf784b76..47d3c29f8 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -472,11 +472,11 @@ fn print_bytes(prefix: &str, input_decoder: &MemoryDecoder, output_info: &Output /// /// `skip_bytes` is the number of bytes skipped from the input /// `read_bytes` is an optional limit to the number of bytes to read -fn open_input_peek_reader<'a>( - input_strings: &'a [String], +fn open_input_peek_reader( + input_strings: &[String], skip_bytes: usize, read_bytes: Option, -) -> PeekReader>> { +) -> PeekReader> { // should return "impl PeekRead + Read + HasError" when supported in (stable) rust let inputs = input_strings .iter() diff --git a/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs b/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs index 33c3a7af5..bf21bf8f9 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/cninetyninehexfloatf.rs @@ -52,8 +52,7 @@ fn get_primitive_hex( last_dec_place: usize, capitalized: bool, ) -> FormatPrimitive { - let mut f: FormatPrimitive = Default::default(); - f.prefix = Some(String::from(if inprefix.sign == -1 { "-0x" } else { "0x" })); + let prefix = Some(String::from(if inprefix.sign == -1 { "-0x" } else { "0x" })); // assign the digits before and after the decimal points // to separate slices. If no digits after decimal point, @@ -97,7 +96,7 @@ fn get_primitive_hex( // conversion. The best way to do it is to just convert the floatnum // directly to base 2 and then at the end translate back to hex. let mantissa = 0; - f.suffix = Some({ + let suffix = Some({ let ind = if capitalized { "P" } else { "p" }; if mantissa >= 0 { format!("{}+{}", ind, mantissa) @@ -105,7 +104,11 @@ fn get_primitive_hex( format!("{}{}", ind, mantissa) } }); - f + FormatPrimitive { + prefix, + suffix, + ..Default::default() + } } fn to_hex(src: &str, before_decimal: bool) -> String { diff --git a/src/uu/printf/src/tokenize/num_format/formatters/intf.rs b/src/uu/printf/src/tokenize/num_format/formatters/intf.rs index 489420c41..9231bd027 100644 --- a/src/uu/printf/src/tokenize/num_format/formatters/intf.rs +++ b/src/uu/printf/src/tokenize/num_format/formatters/intf.rs @@ -198,9 +198,10 @@ impl Formatter for Intf { // We always will have a format primitive to return Some(if convert_hints.len_digits == 0 || convert_hints.is_zero { // if non-digit or end is reached before a non-zero digit - let mut fmt_prim: FormatPrimitive = Default::default(); - fmt_prim.pre_decimal = Some(String::from("0")); - fmt_prim + FormatPrimitive { + pre_decimal: Some(String::from("0")), + ..Default::default() + } } else if !convert_hints.past_max { // if the number is or may be below the bounds limit let radix_out = match *field.field_char { diff --git a/src/uu/realpath/src/realpath.rs b/src/uu/realpath/src/realpath.rs index cdcb8e794..5cc8f3d9a 100644 --- a/src/uu/realpath/src/realpath.rs +++ b/src/uu/realpath/src/realpath.rs @@ -67,7 +67,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let paths: Vec = matches .values_of(ARG_FILES) .unwrap() - .map(|path| PathBuf::from(path)) + .map(PathBuf::from) .collect(); let strip = matches.is_present(OPT_STRIP); diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index 9f4bb01c4..278e872fb 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -97,9 +97,9 @@ With no FILE, or when FILE is -, read standard input.", }; let repeat = matches.opt_present("repeat"); let sep = if matches.opt_present("zero-terminated") { - 0x00 as u8 + 0x00_u8 } else { - 0x0a as u8 + 0x0a_u8 }; let count = match matches.opt_str("head-count") { Some(cnt) => match cnt.parse::() { diff --git a/src/uu/split/src/platform/unix.rs b/src/uu/split/src/platform/unix.rs index f26628174..20d9d637b 100644 --- a/src/uu/split/src/platform/unix.rs +++ b/src/uu/split/src/platform/unix.rs @@ -68,12 +68,13 @@ impl FilterWriter { // set $FILE, save previous value (if there was one) let _with_env_var_set = WithEnvVarSet::new("FILE", &filepath); - let shell_process = Command::new(env::var("SHELL").unwrap_or("/bin/sh".to_owned())) - .arg("-c") - .arg(command) - .stdin(Stdio::piped()) - .spawn() - .expect("Couldn't spawn filter command"); + let shell_process = + Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned())) + .arg("-c") + .arg(command) + .stdin(Stdio::piped()) + .spawn() + .expect("Couldn't spawn filter command"); FilterWriter { shell_process } } diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 851edc4b5..4f80e25a3 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -150,7 +150,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .value_of(OPT_SUFFIX_LENGTH) .unwrap() .parse() - .expect(format!("Invalid number for {}", OPT_SUFFIX_LENGTH).as_str()); + .unwrap_or_else(|_| panic!("Invalid number for {}", OPT_SUFFIX_LENGTH)); settings.numeric_suffix = matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0; settings.additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(); diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index c62d86c26..cd391a53e 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -275,7 +275,11 @@ impl Error for ParseSizeErr { impl fmt::Display for ParseSizeErr { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{}", self.to_string()) + let s = match self { + ParseSizeErr::ParseFailure(s) => s, + ParseSizeErr::SizeTooBig(s) => s, + }; + write!(f, "{}", s) } } diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index a5acd482a..9cd5865b7 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -12,7 +12,6 @@ extern crate uucore; use clap::{App, Arg}; use std::fs::{metadata, File, OpenOptions}; -use std::io::Result; use std::path::Path; #[derive(Eq, PartialEq)] @@ -118,10 +117,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if reference.is_none() && size.is_none() { crash!(1, "you must specify either --reference or --size"); } else { - match truncate(no_create, io_blocks, reference, size, files) { - Ok(()) => ( /* pass */ ), - Err(_) => return 1, - } + truncate(no_create, io_blocks, reference, size, files); } } @@ -134,7 +130,7 @@ fn truncate( reference: Option, size: Option, filenames: Vec, -) -> Result<()> { +) { let (refsize, mode) = match reference { Some(rfilename) => { let _ = match File::open(Path::new(&rfilename)) { @@ -193,7 +189,6 @@ fn truncate( Err(f) => crash!(1, "{}", f.to_string()), } } - Ok(()) } fn parse_size(size: &str) -> (u64, TruncateMode) {