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:
parent
4ddc65f255
commit
d192ebea5b
11 changed files with 24 additions and 24 deletions
|
@ -395,8 +395,8 @@ impl Chowner {
|
||||||
fn wrap_chown<P: AsRef<Path>>(&self, path: P, meta: &Metadata, follow: bool) -> i32 {
|
fn wrap_chown<P: AsRef<Path>>(&self, path: P, meta: &Metadata, follow: bool) -> i32 {
|
||||||
use self::Verbosity::*;
|
use self::Verbosity::*;
|
||||||
let mut ret = 0;
|
let mut ret = 0;
|
||||||
let dest_uid = self.dest_uid.unwrap_or(meta.uid());
|
let dest_uid = self.dest_uid.unwrap_or_else(|| meta.uid());
|
||||||
let dest_gid = self.dest_gid.unwrap_or(meta.gid());
|
let dest_gid = self.dest_gid.unwrap_or_else(|| meta.gid());
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
if let Err(e) = self.chown(path, dest_uid, dest_gid, follow) {
|
if let Err(e) = self.chown(path, dest_uid, dest_gid, follow) {
|
||||||
match self.verbosity {
|
match self.verbosity {
|
||||||
|
|
10
src/id/id.rs
10
src/id/id.rs
|
@ -117,7 +117,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
if nflag {
|
if nflag {
|
||||||
entries::gid2grp(id).unwrap_or(id.to_string())
|
entries::gid2grp(id).unwrap_or_else(|_| id.to_string())
|
||||||
} else {
|
} else {
|
||||||
id.to_string()
|
id.to_string()
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
if nflag {
|
if nflag {
|
||||||
entries::uid2usr(id).unwrap_or(id.to_string())
|
entries::uid2usr(id).unwrap_or_else(|_| id.to_string())
|
||||||
} else {
|
} else {
|
||||||
id.to_string()
|
id.to_string()
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
if nflag {
|
if nflag {
|
||||||
possible_pw
|
possible_pw
|
||||||
.map(|p| p.belongs_to())
|
.map(|p| p.belongs_to())
|
||||||
.unwrap_or(entries::get_groups().unwrap())
|
.unwrap_or_else(|| entries::get_groups().unwrap())
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&id| entries::gid2grp(id).unwrap())
|
.map(|&id| entries::gid2grp(id).unwrap())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
@ -154,7 +154,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
} else {
|
} else {
|
||||||
possible_pw
|
possible_pw
|
||||||
.map(|p| p.belongs_to())
|
.map(|p| p.belongs_to())
|
||||||
.unwrap_or(entries::get_groups().unwrap())
|
.unwrap_or_else(|| entries::get_groups().unwrap())
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&id| id.to_string())
|
.map(|&id| id.to_string())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
@ -258,7 +258,7 @@ fn pline(possible_uid: Option<uid_t>) {
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn pline(possible_uid: Option<uid_t>) {
|
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();
|
let pw = Passwd::locate(uid).unwrap();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
|
|
|
@ -58,7 +58,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
return kill(
|
return kill(
|
||||||
&matches
|
&matches
|
||||||
.opt_str("signal")
|
.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,
|
matches.free,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)]
|
#[cfg(unix)]
|
||||||
lazy_static! {
|
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> = {
|
static ref COLOR_MAP: HashMap<&'static str, &'static str> = {
|
||||||
let codes = LS_COLORS.split(":");
|
let codes = LS_COLORS.split(":");
|
||||||
let mut map = HashMap::new();
|
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") {
|
if options.opt_present("numeric-uid-gid") {
|
||||||
metadata.uid().to_string()
|
metadata.uid().to_string()
|
||||||
} else {
|
} 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") {
|
if options.opt_present("numeric-uid-gid") {
|
||||||
metadata.gid().to_string()
|
metadata.gid().to_string()
|
||||||
} else {
|
} else {
|
||||||
entries::gid2grp(metadata.gid()).unwrap_or(metadata.gid().to_string())
|
entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ FILE, separated by TABs, to standard output.",
|
||||||
println!("{} {}", NAME, VERSION);
|
println!("{} {}", NAME, VERSION);
|
||||||
} else {
|
} else {
|
||||||
let serial = matches.opt_present("serial");
|
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);
|
paste(matches.free, serial, delimiters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -254,7 +254,7 @@ impl Pinky {
|
||||||
let ut_host = ut.host();
|
let ut_host = ut.host();
|
||||||
let mut res = ut_host.splitn(2, ':');
|
let mut res = ut_host.splitn(2, ':');
|
||||||
let host = match res.next() {
|
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(),
|
None => ut_host.clone(),
|
||||||
};
|
};
|
||||||
match res.next() {
|
match res.next() {
|
||||||
|
|
|
@ -233,7 +233,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
};
|
};
|
||||||
let last = {
|
let last = {
|
||||||
let slice = &free[free.len() - 1][..];
|
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) {
|
match parse_float(slice) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
Err(s) => {
|
Err(s) => {
|
||||||
|
|
|
@ -26,7 +26,7 @@ impl BirthTime for Metadata {
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).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))
|
.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 {
|
fn birth(&self) -> String {
|
||||||
|
@ -34,7 +34,7 @@ impl BirthTime for Metadata {
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
|
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
|
||||||
.map(|e| format!("{}", e.as_secs()))
|
.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);
|
let errno = IOError::last_os_error().raw_os_error().unwrap_or(0);
|
||||||
Err(CString::from_raw(strerror(errno))
|
Err(CString::from_raw(strerror(errno))
|
||||||
.into_string()
|
.into_string()
|
||||||
.unwrap_or("Unknown Error".to_owned()))
|
.unwrap_or_else(|_| "Unknown Error".to_owned()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(e) => Err(e.description().to_owned()),
|
Err(e) => Err(e.description().to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -458,7 +458,7 @@ impl Stater {
|
||||||
let fmtstr = if matches.opt_present("printf") {
|
let fmtstr = if matches.opt_present("printf") {
|
||||||
matches.opt_str("printf").expect("Invalid format string")
|
matches.opt_str("printf").expect("Invalid format string")
|
||||||
} else {
|
} 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");
|
let use_printf = matches.opt_present("printf");
|
||||||
|
@ -478,7 +478,7 @@ impl Stater {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
let reader = BufReader::new(
|
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
|
let mut mount_list = reader
|
||||||
.lines()
|
.lines()
|
||||||
|
@ -608,7 +608,7 @@ impl Stater {
|
||||||
// group name of owner
|
// group name of owner
|
||||||
'G' => {
|
'G' => {
|
||||||
arg = entries::gid2grp(meta.gid())
|
arg = entries::gid2grp(meta.gid())
|
||||||
.unwrap_or("UNKNOWN".to_owned());
|
.unwrap_or_else(|_| "UNKNOWN".to_owned());
|
||||||
otype = OutputType::Str;
|
otype = OutputType::Str;
|
||||||
}
|
}
|
||||||
// number of hard links
|
// number of hard links
|
||||||
|
@ -683,7 +683,7 @@ impl Stater {
|
||||||
// user name of owner
|
// user name of owner
|
||||||
'U' => {
|
'U' => {
|
||||||
arg = entries::uid2usr(meta.uid())
|
arg = entries::uid2usr(meta.uid())
|
||||||
.unwrap_or("UNKNOWN".to_owned());
|
.unwrap_or_else(|_| "UNKNOWN".to_owned());
|
||||||
otype = OutputType::Str;
|
otype = OutputType::Str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -316,7 +316,7 @@ pub fn parse_size(mut size_slice: &str) -> Result<u64, ParseSizeErr> {
|
||||||
let value: Option<u64> = size_slice.parse().ok();
|
let value: Option<u64> = size_slice.parse().ok();
|
||||||
value
|
value
|
||||||
.map(|v| Ok(multiplier * v))
|
.map(|v| Ok(multiplier * v))
|
||||||
.unwrap_or(Err(ParseSizeErr::parse_failure(size_slice)))
|
.unwrap_or_else(|| Err(ParseSizeErr::parse_failure(size_slice)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -463,7 +463,7 @@ impl Who {
|
||||||
let mut res = ut_host.splitn(2, ':');
|
let mut res = ut_host.splitn(2, ':');
|
||||||
if let Some(h) = res.next() {
|
if let Some(h) = res.next() {
|
||||||
if self.do_lookup {
|
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 {
|
} else {
|
||||||
buf.push(h.to_owned());
|
buf.push(h.to_owned());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue