1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #2387 from youknowone/unwrap

Replace trivial unwraps to different expressions
This commit is contained in:
Sylvestre Ledru 2021-06-10 10:13:21 +02:00 committed by GitHub
commit 8a80109ce5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 61 deletions

View file

@ -38,18 +38,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let config_result: Result<base_common::Config, String> =
base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage);
if config_result.is_err() {
match config_result {
Ok(_) => panic!(),
Err(s) => crash!(BASE_CMD_PARSE_ERROR, "{}", s),
}
}
let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s));
// Create a reference to stdin so we can return a locked stdin from
// parse_base_cmd_args
let stdin_raw = stdin();
let config = config_result.unwrap();
let mut input: Box<dyn Read> = base_common::get_input(&config, &stdin_raw);
base_common::handle_input(

View file

@ -38,18 +38,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let name = executable!();
let config_result: Result<base_common::Config, String> =
base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage);
if config_result.is_err() {
match config_result {
Ok(_) => panic!(),
Err(s) => crash!(BASE_CMD_PARSE_ERROR, "{}", s),
}
}
let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s));
// Create a reference to stdin so we can return a locked stdin from
// parse_base_cmd_args
let stdin_raw = stdin();
let config = config_result.unwrap();
let mut input: Box<dyn Read> = base_common::get_input(&config, &stdin_raw);
base_common::handle_input(

View file

@ -134,23 +134,25 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
Err(err) => crash!(1, "cannot stat attributes of '{}': {}", fref, err),
});
let modes = matches.value_of(options::MODE).unwrap(); // should always be Some because required
let mut cmode = if mode_had_minus_prefix {
let cmode = if mode_had_minus_prefix {
// clap parsing is finished, now put prefix back
Some(format!("-{}", modes))
format!("-{}", modes)
} else {
Some(modes.to_string())
modes.to_string()
};
let mut files: Vec<String> = matches
.values_of(options::FILE)
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();
if fmode.is_some() {
let cmode = if fmode.is_some() {
// "--reference" and MODE are mutually exclusive
// if "--reference" was used MODE needs to be interpreted as another FILE
// it wasn't possible to implement this behavior directly with clap
files.push(cmode.unwrap());
cmode = None;
}
files.push(cmode);
None
} else {
Some(cmode)
};
let chmoder = Chmoder {
changes,

View file

@ -320,8 +320,7 @@ fn du(
if this_stat.is_dir {
futures.push(du(this_stat, options, depth + 1, inodes));
} else {
if this_stat.inode.is_some() {
let inode = this_stat.inode.unwrap();
if let Some(inode) = this_stat.inode {
if inodes.contains(&inode) {
continue;
}
@ -360,7 +359,9 @@ fn du(
my_stat.size += stat.size;
my_stat.blocks += stat.blocks;
}
options.max_depth == None || depth < options.max_depth.unwrap()
options
.max_depth
.map_or(true, |max_depth| depth < max_depth)
}));
stats.push(my_stat);
Box::new(stats.into_iter())

View file

@ -56,11 +56,7 @@ fn print_expr_error(expr_error: &str) -> ! {
}
fn evaluate_ast(maybe_ast: Result<Box<syntax_tree::AstNode>, String>) -> Result<String, String> {
if maybe_ast.is_err() {
Err(maybe_ast.err().unwrap())
} else {
maybe_ast.ok().unwrap().evaluate()
}
maybe_ast.and_then(|ast| ast.evaluate())
}
fn maybe_handle_help_or_version(args: &[String]) -> bool {

View file

@ -175,23 +175,14 @@ impl AstNode {
pub fn tokens_to_ast(
maybe_tokens: Result<Vec<(usize, Token)>, String>,
) -> Result<Box<AstNode>, String> {
if maybe_tokens.is_err() {
Err(maybe_tokens.err().unwrap())
} else {
let tokens = maybe_tokens.ok().unwrap();
maybe_tokens.and_then(|tokens| {
let mut out_stack: TokenStack = Vec::new();
let mut op_stack: TokenStack = Vec::new();
for (token_idx, token) in tokens {
if let Err(reason) =
push_token_to_either_stack(token_idx, &token, &mut out_stack, &mut op_stack)
{
return Err(reason);
}
}
if let Err(reason) = move_rest_of_ops_to_out(&mut out_stack, &mut op_stack) {
return Err(reason);
push_token_to_either_stack(token_idx, &token, &mut out_stack, &mut op_stack)?;
}
move_rest_of_ops_to_out(&mut out_stack, &mut op_stack)?;
assert!(op_stack.is_empty());
maybe_dump_rpn(&out_stack);
@ -205,7 +196,7 @@ pub fn tokens_to_ast(
maybe_dump_ast(&result);
result
}
}
})
}
fn maybe_dump_ast(result: &Result<Box<AstNode>, String>) {

View file

@ -98,7 +98,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
for (i, arg) in args.iter().enumerate() {
let slice = &arg;
if slice.starts_with('-') && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) {
if slice.starts_with('-') && slice.chars().nth(1).map_or(false, |c| c.is_digit(10)) {
let mut v = args.to_vec();
v.remove(i);
return (v, Some(slice[1..].to_owned()));

View file

@ -255,10 +255,8 @@ fn detect_algo<'a>(
}
}
}
if alg.is_none() {
crash!(1, "You must specify hash algorithm!")
};
(name, alg.unwrap(), output_bits)
let alg = alg.unwrap_or_else(|| crash!(1, "You must specify hash algorithm!"));
(name, alg, output_bits)
}
}
}

View file

@ -111,7 +111,7 @@ fn handle_obsolete(mut args: Vec<String>) -> (Vec<String>, Option<String>) {
while i < args.len() {
// this is safe because slice is valid when it is referenced
let slice = &args[i].clone();
if slice.starts_with('-') && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) {
if slice.starts_with('-') && slice.chars().nth(1).map_or(false, |c| c.is_digit(10)) {
let val = &slice[1..];
match val.parse() {
Ok(num) => {

View file

@ -275,17 +275,13 @@ fn parse_type_string(params: &str) -> Result<Vec<ParsedFormatterItemInfo>, Strin
let mut chars = params.chars();
let mut ch = chars.next();
while ch.is_some() {
let type_char = ch.unwrap();
let type_char = match format_type(type_char) {
Some(t) => t,
None => {
return Err(format!(
"unexpected char '{}' in format specification '{}'",
type_char, params
));
}
};
while let Some(type_char) = ch {
let type_char = format_type(type_char).ok_or_else(|| {
format!(
"unexpected char '{}' in format specification '{}'",
type_char, params
)
})?;
let type_cat = format_type_category(type_char);