1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-04 15:07:47 +00:00

refactor/polish ~ fix cargo clippy complaints (unwrap_or_else)

This commit is contained in:
Roy Ivy III 2019-12-26 21:39:42 -06:00
parent 4ddc65f255
commit d192ebea5b
11 changed files with 24 additions and 24 deletions

View file

@ -395,8 +395,8 @@ impl Chowner {
fn wrap_chown<P: AsRef<Path>>(&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 {

View file

@ -117,7 +117,7 @@ pub fn uumain(args: Vec<String>) -> 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<String>) -> 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<String>) -> 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::<Vec<_>>()
@ -154,7 +154,7 @@ pub fn uumain(args: Vec<String>) -> 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::<Vec<_>>()
@ -258,7 +258,7 @@ fn pline(possible_uid: Option<uid_t>) {
#[cfg(target_os = "linux")]
fn pline(possible_uid: Option<uid_t>) {
let uid = possible_uid.unwrap_or(getuid());
let uid = possible_uid.unwrap_or_else(getuid);
let pw = Passwd::locate(uid).unwrap();
println!(

View file

@ -58,7 +58,7 @@ pub fn uumain(args: Vec<String>) -> 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,
)
}

View file

@ -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())
}
}

View file

@ -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);
}

View file

@ -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() {

View file

@ -233,7 +233,7 @@ pub fn uumain(args: Vec<String>) -> 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) => {

View file

@ -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,7 +260,7 @@ 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()))
}
}
}

View file

@ -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;
}

View file

@ -316,7 +316,7 @@ pub fn parse_size(mut size_slice: &str) -> Result<u64, ParseSizeErr> {
let value: Option<u64> = 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)))
}
}

View file

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