From d192ebea5b8a0e4582c4bbac2c287ebbdff3a17c Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Thu, 26 Dec 2019 21:39:42 -0600 Subject: [PATCH] refactor/polish ~ fix `cargo clippy` complaints (unwrap_or_else) --- src/chown/chown.rs | 4 ++-- src/id/id.rs | 10 +++++----- src/kill/kill.rs | 2 +- src/ls/ls.rs | 6 +++--- src/paste/paste.rs | 2 +- src/pinky/pinky.rs | 2 +- src/seq/seq.rs | 2 +- src/stat/fsext.rs | 8 ++++---- src/stat/stat.rs | 8 ++++---- src/tail/tail.rs | 2 +- src/who/who.rs | 2 +- 11 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/chown/chown.rs b/src/chown/chown.rs index ef6086c2c..22d45938a 100644 --- a/src/chown/chown.rs +++ b/src/chown/chown.rs @@ -395,8 +395,8 @@ impl Chowner { fn wrap_chown>(&self, path: P, meta: &Metadata, follow: bool) -> i32 { use self::Verbosity::*; let mut ret = 0; - let dest_uid = self.dest_uid.unwrap_or(meta.uid()); - let dest_gid = self.dest_gid.unwrap_or(meta.gid()); + let dest_uid = self.dest_uid.unwrap_or_else(|| meta.uid()); + let dest_gid = self.dest_gid.unwrap_or_else(|| meta.gid()); let path = path.as_ref(); if let Err(e) = self.chown(path, dest_uid, dest_gid, follow) { match self.verbosity { diff --git a/src/id/id.rs b/src/id/id.rs index 0b87fb5ec..a0e59fc73 100644 --- a/src/id/id.rs +++ b/src/id/id.rs @@ -117,7 +117,7 @@ pub fn uumain(args: Vec) -> i32 { println!( "{}", if nflag { - entries::gid2grp(id).unwrap_or(id.to_string()) + entries::gid2grp(id).unwrap_or_else(|_| id.to_string()) } else { id.to_string() } @@ -132,7 +132,7 @@ pub fn uumain(args: Vec) -> i32 { println!( "{}", if nflag { - entries::uid2usr(id).unwrap_or(id.to_string()) + entries::uid2usr(id).unwrap_or_else(|_| id.to_string()) } else { id.to_string() } @@ -146,7 +146,7 @@ pub fn uumain(args: Vec) -> i32 { if nflag { possible_pw .map(|p| p.belongs_to()) - .unwrap_or(entries::get_groups().unwrap()) + .unwrap_or_else(|| entries::get_groups().unwrap()) .iter() .map(|&id| entries::gid2grp(id).unwrap()) .collect::>() @@ -154,7 +154,7 @@ pub fn uumain(args: Vec) -> i32 { } else { possible_pw .map(|p| p.belongs_to()) - .unwrap_or(entries::get_groups().unwrap()) + .unwrap_or_else(|| entries::get_groups().unwrap()) .iter() .map(|&id| id.to_string()) .collect::>() @@ -258,7 +258,7 @@ fn pline(possible_uid: Option) { #[cfg(target_os = "linux")] fn pline(possible_uid: Option) { - let uid = possible_uid.unwrap_or(getuid()); + let uid = possible_uid.unwrap_or_else(getuid); let pw = Passwd::locate(uid).unwrap(); println!( diff --git a/src/kill/kill.rs b/src/kill/kill.rs index 5250836c6..483681e7a 100644 --- a/src/kill/kill.rs +++ b/src/kill/kill.rs @@ -58,7 +58,7 @@ pub fn uumain(args: Vec) -> i32 { return kill( &matches .opt_str("signal") - .unwrap_or(obs_signal.unwrap_or("9".to_owned())), + .unwrap_or_else(|| obs_signal.unwrap_or_else(|| "9".to_owned())), matches.free, ) } diff --git a/src/ls/ls.rs b/src/ls/ls.rs index 02ebe87e5..9bbc8b338 100644 --- a/src/ls/ls.rs +++ b/src/ls/ls.rs @@ -60,7 +60,7 @@ static DEFAULT_COLORS: &str = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do #[cfg(unix)] lazy_static! { - static ref LS_COLORS: String = std::env::var("LS_COLORS").unwrap_or(DEFAULT_COLORS.to_string()); + static ref LS_COLORS: String = std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string()); static ref COLOR_MAP: HashMap<&'static str, &'static str> = { let codes = LS_COLORS.split(":"); let mut map = HashMap::new(); @@ -454,7 +454,7 @@ fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String { if options.opt_present("numeric-uid-gid") { metadata.uid().to_string() } else { - entries::uid2usr(metadata.uid()).unwrap_or(metadata.uid().to_string()) + entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string()) } } @@ -463,7 +463,7 @@ fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String { if options.opt_present("numeric-uid-gid") { metadata.gid().to_string() } else { - entries::gid2grp(metadata.gid()).unwrap_or(metadata.gid().to_string()) + entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string()) } } diff --git a/src/paste/paste.rs b/src/paste/paste.rs index bffe0b96f..59e4762ee 100644 --- a/src/paste/paste.rs +++ b/src/paste/paste.rs @@ -60,7 +60,7 @@ FILE, separated by TABs, to standard output.", println!("{} {}", NAME, VERSION); } else { let serial = matches.opt_present("serial"); - let delimiters = matches.opt_str("delimiters").unwrap_or("\t".to_owned()); + let delimiters = matches.opt_str("delimiters").unwrap_or_else(|| "\t".to_owned()); paste(matches.free, serial, delimiters); } diff --git a/src/pinky/pinky.rs b/src/pinky/pinky.rs index 73c97fbbb..97cd00f9a 100644 --- a/src/pinky/pinky.rs +++ b/src/pinky/pinky.rs @@ -254,7 +254,7 @@ impl Pinky { let ut_host = ut.host(); let mut res = ut_host.splitn(2, ':'); let host = match res.next() { - Some(_) => ut.canon_host().unwrap_or(ut_host.clone()), + Some(_) => ut.canon_host().unwrap_or_else(|_| ut_host.clone()), None => ut_host.clone(), }; match res.next() { diff --git a/src/seq/seq.rs b/src/seq/seq.rs index 4bdbbf370..4aa60aa64 100644 --- a/src/seq/seq.rs +++ b/src/seq/seq.rs @@ -233,7 +233,7 @@ pub fn uumain(args: Vec) -> i32 { }; let last = { let slice = &free[free.len() - 1][..]; - padding = cmp::max(padding, slice.find('.').unwrap_or(slice.len())); + padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len())); match parse_float(slice) { Ok(n) => n, Err(s) => { diff --git a/src/stat/fsext.rs b/src/stat/fsext.rs index 851361b09..7f3f5e268 100644 --- a/src/stat/fsext.rs +++ b/src/stat/fsext.rs @@ -26,7 +26,7 @@ impl BirthTime for Metadata { .ok() .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) .map(|e| pretty_time(e.as_secs() as i64, e.subsec_nanos() as i64)) - .unwrap_or("-".to_owned()) + .unwrap_or_else(|| "-".to_owned()) } fn birth(&self) -> String { @@ -34,7 +34,7 @@ impl BirthTime for Metadata { .ok() .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) .map(|e| format!("{}", e.as_secs())) - .unwrap_or("0".to_owned()) + .unwrap_or_else(|| "0".to_owned()) } } @@ -260,11 +260,11 @@ where let errno = IOError::last_os_error().raw_os_error().unwrap_or(0); Err(CString::from_raw(strerror(errno)) .into_string() - .unwrap_or("Unknown Error".to_owned())) + .unwrap_or_else(|_| "Unknown Error".to_owned())) + } } } } - } Err(e) => Err(e.description().to_owned()), } } diff --git a/src/stat/stat.rs b/src/stat/stat.rs index e8fd4165c..6d0eff468 100644 --- a/src/stat/stat.rs +++ b/src/stat/stat.rs @@ -458,7 +458,7 @@ impl Stater { let fmtstr = if matches.opt_present("printf") { matches.opt_str("printf").expect("Invalid format string") } else { - matches.opt_str("format").unwrap_or("".to_owned()) + matches.opt_str("format").unwrap_or_else(|| "".to_owned()) }; let use_printf = matches.opt_present("printf"); @@ -478,7 +478,7 @@ impl Stater { None } else { let reader = BufReader::new( - File::open(MOUNT_INFO).expect(&format!("Failed to read {}", MOUNT_INFO)), + File::open(MOUNT_INFO).unwrap_or_else(|_| panic!("Failed to read {}", MOUNT_INFO)), ); let mut mount_list = reader .lines() @@ -608,7 +608,7 @@ impl Stater { // group name of owner 'G' => { arg = entries::gid2grp(meta.gid()) - .unwrap_or("UNKNOWN".to_owned()); + .unwrap_or_else(|_| "UNKNOWN".to_owned()); otype = OutputType::Str; } // number of hard links @@ -683,7 +683,7 @@ impl Stater { // user name of owner 'U' => { arg = entries::uid2usr(meta.uid()) - .unwrap_or("UNKNOWN".to_owned()); + .unwrap_or_else(|_| "UNKNOWN".to_owned()); otype = OutputType::Str; } diff --git a/src/tail/tail.rs b/src/tail/tail.rs index c6bbbfdf2..a1ed021c4 100755 --- a/src/tail/tail.rs +++ b/src/tail/tail.rs @@ -316,7 +316,7 @@ pub fn parse_size(mut size_slice: &str) -> Result { let value: Option = size_slice.parse().ok(); value .map(|v| Ok(multiplier * v)) - .unwrap_or(Err(ParseSizeErr::parse_failure(size_slice))) + .unwrap_or_else(|| Err(ParseSizeErr::parse_failure(size_slice))) } } diff --git a/src/who/who.rs b/src/who/who.rs index b8fe3eb82..68972be1f 100644 --- a/src/who/who.rs +++ b/src/who/who.rs @@ -463,7 +463,7 @@ impl Who { let mut res = ut_host.splitn(2, ':'); if let Some(h) = res.next() { if self.do_lookup { - buf.push(ut.canon_host().unwrap_or(h.to_owned())); + buf.push(ut.canon_host().unwrap_or_else(|_| h.to_owned())); } else { buf.push(h.to_owned()); }