From 8ef7f394c1eeeaa82f428741f247edef255f708b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 29 Nov 2020 16:26:38 +0100 Subject: [PATCH] address some new clippy warnings (#1642) --- src/uu/chroot/src/chroot.rs | 14 ++++++-------- src/uu/comm/src/comm.rs | 2 +- src/uu/expr/src/expr.rs | 2 +- src/uu/join/src/join.rs | 5 +---- src/uu/mkdir/src/mkdir.rs | 2 +- src/uu/mktemp/src/mktemp.rs | 2 +- src/uu/nice/src/nice.rs | 2 +- src/uu/nl/src/nl.rs | 2 +- src/uu/nproc/src/nproc.rs | 5 +---- src/uu/od/src/od.rs | 5 +---- .../printf/src/tokenize/num_format/num_format.rs | 4 ++-- src/uu/printf/src/tokenize/unescaped_text.rs | 5 +---- src/uu/sort/src/sort.rs | 5 +---- src/uu/stat/src/stat.rs | 2 +- src/uu/uname/src/uname.rs | 12 ++++++------ src/uu/wc/src/wc.rs | 4 ++-- 16 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 89da2109e..c0205406a 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -17,7 +17,6 @@ use uucore::libc::{self, chroot, setgid, setgroups, setuid}; use std::ffi::CString; use std::io::Error; -use std::iter::FromIterator; use std::path::Path; use std::process::Command; @@ -96,10 +95,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if pstatus.success() { 0 } else { - match pstatus.code() { - Some(i) => i, - None => -1, - } + pstatus.code().unwrap_or(-1) } } @@ -182,11 +178,13 @@ fn set_groups(groups: Vec) -> libc::c_int { fn set_groups_from_str(groups: &str) { if !groups.is_empty() { - let groups_vec: Vec = - FromIterator::from_iter(groups.split(',').map(|x| match entries::grp2gid(x) { + let groups_vec: Vec = groups + .split(',') + .map(|x| match entries::grp2gid(x) { Ok(g) => g, _ => crash!(1, "no such group: {}", x), - })); + }) + .collect(); let err = set_groups(groups_vec); if err != 0 { crash!(1, "cannot set groups: {}", Error::last_os_error()) diff --git a/src/uu/comm/src/comm.rs b/src/uu/comm/src/comm.rs index 8c43f3a17..4e15ce9c9 100644 --- a/src/uu/comm/src/comm.rs +++ b/src/uu/comm/src/comm.rs @@ -41,7 +41,7 @@ fn mkdelim(col: usize, opts: &getopts::Matches) -> String { fn ensure_nl(line: &mut String) { match line.chars().last() { Some('\n') => (), - _ => line.push_str("\n"), + _ => line.push('\n'), } } diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index db970f8a6..b63f8b6a8 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -41,7 +41,7 @@ fn process_expr(token_strings: &[String]) -> Result { fn print_expr_ok(expr_result: &str) -> i32 { println!("{}", expr_result); - if expr_result == "0" || expr_result == "" { + if expr_result == "0" || expr_result.is_empty() { 1 } else { 0 diff --git a/src/uu/join/src/join.rs b/src/uu/join/src/join.rs index 23feae662..cbce1be84 100644 --- a/src/uu/join/src/join.rs +++ b/src/uu/join/src/join.rs @@ -696,10 +696,7 @@ fn get_field_number(keys: Option, key: Option) -> usize { return keys; } - match key { - Some(key) => key, - None => 0, - } + key.unwrap_or(0) } /// Parse the specified field string as a natural number and return diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index 7e53ed8e7..ae95e2126 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -85,7 +85,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { _ => crash!(1, "no mode given"), } } - _ => 0o755 as u16, + _ => 0o755_u16, }; exec(dirs, recursive, mode, verbose) diff --git a/src/uu/mktemp/src/mktemp.rs b/src/uu/mktemp/src/mktemp.rs index 8cae1f70d..508f21d2f 100644 --- a/src/uu/mktemp/src/mktemp.rs +++ b/src/uu/mktemp/src/mktemp.rs @@ -96,7 +96,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let (prefix, rand, suffix) = match parse_template(template) { Some((p, r, s)) => match suffix_opt { Some(suf) => { - if s == "" { + if s.is_empty() { (p, r, suf) } else { crash!( diff --git a/src/uu/nice/src/nice.rs b/src/uu/nice/src/nice.rs index 5d5647a51..52fb287b1 100644 --- a/src/uu/nice/src/nice.rs +++ b/src/uu/nice/src/nice.rs @@ -103,7 +103,7 @@ process).", println!("{}", niceness); return 0; } - 10 as c_int + 10_i32 } }; diff --git a/src/uu/nl/src/nl.rs b/src/uu/nl/src/nl.rs index 6bf134704..bc8c62016 100644 --- a/src/uu/nl/src/nl.rs +++ b/src/uu/nl/src/nl.rs @@ -310,7 +310,7 @@ fn nl(reader: &mut BufReader, settings: &Settings) { continue; } // From this point on we format and print a "regular" line. - if line == "" { + if line.is_empty() { // The line is empty, which means that we have to care // about the --join-blank-lines parameter. empty_line_count += 1; diff --git a/src/uu/nproc/src/nproc.rs b/src/uu/nproc/src/nproc.rs index ec8aaf90a..d1dec9005 100644 --- a/src/uu/nproc/src/nproc.rs +++ b/src/uu/nproc/src/nproc.rs @@ -73,10 +73,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if !matches.is_present(OPT_ALL) { // OMP_NUM_THREADS doesn't have an impact on --all ignore += match env::var("OMP_NUM_THREADS") { - Ok(threadstr) => match threadstr.parse() { - Ok(num) => num, - Err(_) => 0, - }, + Ok(threadstr) => threadstr.parse().unwrap_or(0), Err(_) => 0, }; } diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 84848a72c..64371bbe1 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -229,10 +229,7 @@ impl OdOptions { let mut line_bytes = match matches.opt_default("w", "32") { None => 16, - Some(s) => match s.parse::() { - Ok(i) => i, - Err(_) => 0, - }, + Some(s) => s.parse::().unwrap_or(0), }; let min_bytes = formats.iter().fold(1, |max, next| { cmp::max(max, next.formatter_item_info.byte_size) diff --git a/src/uu/printf/src/tokenize/num_format/num_format.rs b/src/uu/printf/src/tokenize/num_format/num_format.rs index 44afd2e66..9a519e95e 100644 --- a/src/uu/printf/src/tokenize/num_format/num_format.rs +++ b/src/uu/printf/src/tokenize/num_format/num_format.rs @@ -64,7 +64,7 @@ fn get_provided(str_in_opt: Option<&String>) -> Option { None => { let so_far = (qchar as u8 as char).to_string(); warn_expected_numeric(&so_far); - 0 as u8 + 0_u8 } }) } @@ -72,7 +72,7 @@ fn get_provided(str_in_opt: Option<&String>) -> Option { _ => None, // no first byte } } else { - Some(0 as u8) + Some(0_u8) } } None => Some(0), diff --git a/src/uu/printf/src/tokenize/unescaped_text.rs b/src/uu/printf/src/tokenize/unescaped_text.rs index b6fdd1aa1..3b9f0123e 100644 --- a/src/uu/printf/src/tokenize/unescaped_text.rs +++ b/src/uu/printf/src/tokenize/unescaped_text.rs @@ -85,10 +85,7 @@ impl UnescapedText { // to the passed byte_vec // in subs_mode change octal behavior fn handle_escaped(byte_vec: &mut Vec, it: &mut PutBackN, subs_mode: bool) { - let ch = match it.next() { - Some(x) => x, - None => '\\', - }; + let ch = it.next().unwrap_or('\\'); match ch { '0'..='9' | 'x' => { let min_len = 1; diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 5e88c7d3b..60e0b4127 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -478,10 +478,7 @@ fn numeric_compare(a: &str, b: &str) -> Ordering { fn human_numeric_convert(a: &str) -> f64 { let int_str: String = a.chars().take_while(|c| c.is_numeric()).collect(); let suffix = a.chars().find(|c| !c.is_numeric()); - let int_part = match int_str.parse::() { - Ok(i) => i, - Err(_) => -1f64, - } as f64; + let int_part = int_str.parse::().unwrap_or(-1f64) as f64; let suffix: f64 = match suffix.unwrap_or('\0') { 'K' => 1000f64, 'M' => 1E6, diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 4c3384d4b..a9588dee0 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -469,7 +469,7 @@ impl Stater { let fmtstr = if matches.is_present(OPT_PRINTF) { matches.value_of(OPT_PRINTF).expect("Invalid format string") } else { - matches.value_of(OPT_FORMAT).unwrap_or_else(|| "") + matches.value_of(OPT_FORMAT).unwrap_or("") }; let use_printf = matches.is_present(OPT_PRINTF); diff --git a/src/uu/uname/src/uname.rs b/src/uu/uname/src/uname.rs index c81c9cfa1..6b25292c5 100644 --- a/src/uu/uname/src/uname.rs +++ b/src/uu/uname/src/uname.rs @@ -111,28 +111,28 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if kernelname || all || none { output.push_str(&uname.sysname()); - output.push_str(" "); + output.push(' '); } if nodename || all { output.push_str(&uname.nodename()); - output.push_str(" "); + output.push(' '); } if kernelrelease || all { output.push_str(&uname.release()); - output.push_str(" "); + output.push(' '); } if kernelversion || all { output.push_str(&uname.version()); - output.push_str(" "); + output.push(' '); } if machine || all { output.push_str(&uname.machine()); - output.push_str(" "); + output.push(' '); } if os || all { output.push_str(HOST_OS); - output.push_str(" "); + output.push(' '); } println!("{}", output.trim_end()); diff --git a/src/uu/wc/src/wc.rs b/src/uu/wc/src/wc.rs index 78bc6c2de..a0687e251 100644 --- a/src/uu/wc/src/wc.rs +++ b/src/uu/wc/src/wc.rs @@ -148,8 +148,8 @@ const CR: u8 = b'\r'; const LF: u8 = b'\n'; const SPACE: u8 = b' '; const TAB: u8 = b'\t'; -const SYN: u8 = 0x16 as u8; -const FF: u8 = 0x0C as u8; +const SYN: u8 = 0x16_u8; +const FF: u8 = 0x0C_u8; #[inline(always)] fn is_word_separator(byte: u8) -> bool {