mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-15 22:44:01 +00:00
builtins: Remove some uses of .unwrap()
.unwrap() is in effect an assert(). If it is applied mistakenly, the program crashes and there isn't a good error. I would like it to be used as a last resort. In these cases there are nicer ways to do it that handle a missing result properly.
This commit is contained in:
parent
28d9f1878d
commit
1e5a585875
4 changed files with 17 additions and 21 deletions
|
@ -49,8 +49,8 @@ fn parse_cmd_opts(
|
|||
if optarg == "max" {
|
||||
opts.scale = 15;
|
||||
} else {
|
||||
let scale = fish_wcstoi(optarg);
|
||||
if scale.is_err() || scale.unwrap() < 0 || scale.unwrap() > 15 {
|
||||
let scale = fish_wcstoi(optarg).unwrap_or(-1);
|
||||
if scale < 0 || scale > 15 {
|
||||
streams.err.append(wgettext_fmt!(
|
||||
"%ls: %ls: invalid base value\n",
|
||||
cmd,
|
||||
|
@ -59,7 +59,7 @@ fn parse_cmd_opts(
|
|||
return Err(STATUS_INVALID_ARGS);
|
||||
}
|
||||
// We know the value is in the range [0, 15]
|
||||
opts.scale = scale.unwrap() as usize;
|
||||
opts.scale = scale as usize;
|
||||
}
|
||||
}
|
||||
'b' => {
|
||||
|
@ -69,8 +69,8 @@ fn parse_cmd_opts(
|
|||
} else if optarg == "octal" {
|
||||
opts.base = 8;
|
||||
} else {
|
||||
let base = fish_wcstoi(optarg);
|
||||
if base.is_err() || (base.unwrap() != 8 && base.unwrap() != 16) {
|
||||
let base = fish_wcstoi(optarg).unwrap_or(-1);
|
||||
if base != 8 && base != 16 {
|
||||
streams.err.append(wgettext_fmt!(
|
||||
"%ls: %ls: invalid base value\n",
|
||||
cmd,
|
||||
|
@ -79,7 +79,7 @@ fn parse_cmd_opts(
|
|||
return Err(STATUS_INVALID_ARGS);
|
||||
}
|
||||
// We know the value is 8 or 16.
|
||||
opts.base = base.unwrap() as usize;
|
||||
opts.base = base as usize;
|
||||
}
|
||||
}
|
||||
'h' => {
|
||||
|
|
|
@ -747,7 +747,7 @@ fn filter_path(opts: &Options, path: &wstr, uid: Option<u32>, gid: Option<u32>)
|
|||
let mut type_ok = false;
|
||||
if t.contains(TypeFlags::LINK) {
|
||||
let md = lwstat(path);
|
||||
type_ok = md.is_ok() && md.unwrap().is_symlink();
|
||||
type_ok = md.map(|x| x.is_symlink()).unwrap_or(false);
|
||||
}
|
||||
let Ok(md) = wstat(path) else {
|
||||
// Does not exist
|
||||
|
|
|
@ -425,17 +425,15 @@ mod test_expressions {
|
|||
}
|
||||
|
||||
// Parse another expression.
|
||||
let expr = self.parse_unary_expression(idx, end);
|
||||
if expr.is_none() {
|
||||
let Some(expr) = self.parse_unary_expression(idx, end) else {
|
||||
self.add_error(idx, sprintf!("Missing argument at index %u", idx + 1));
|
||||
if !first {
|
||||
// Clean up the dangling combiner, since it never got its right hand expression.
|
||||
combiners.pop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
// Go to the end of this expression.
|
||||
let expr = expr.unwrap();
|
||||
idx = expr.range().end;
|
||||
subjects.push(expr);
|
||||
first = false;
|
||||
|
@ -773,11 +771,10 @@ mod test_expressions {
|
|||
fn parse_number(arg: &wstr, number: &mut Number, errors: &mut Vec<WString>) -> bool {
|
||||
let floating = parse_double(arg);
|
||||
let integral: Result<i64, Error> = fish_wcstol(arg);
|
||||
let got_int = integral.is_ok();
|
||||
if got_int {
|
||||
if let Ok(int) = integral {
|
||||
// Here the value is just an integer; ignore the floating point parse because it may be
|
||||
// invalid (e.g. not a representable integer).
|
||||
*number = Number::new(integral.unwrap(), 0.0);
|
||||
*number = Number::new(int, 0.0);
|
||||
true
|
||||
} else if floating.is_ok()
|
||||
&& integral.unwrap_err() != Error::Overflow
|
||||
|
@ -816,10 +813,10 @@ mod test_expressions {
|
|||
} else {
|
||||
errors.push(wgettext_fmt!("Argument is not a number: '%ls'", arg));
|
||||
}
|
||||
} else if floating.is_ok() && floating.unwrap().is_nan() {
|
||||
} else if floating.map_or(false, |x| x.is_nan()) {
|
||||
// NaN is an error as far as we're concerned.
|
||||
errors.push(wgettext!("Not a number").to_owned());
|
||||
} else if floating.is_ok() && floating.unwrap().is_infinite() {
|
||||
} else if floating.map_or(false, |x| x.is_infinite()) {
|
||||
errors.push(wgettext!("Number is infinite").to_owned());
|
||||
} else if integral == Err(Error::Overflow) {
|
||||
errors.push(wgettext_fmt!("Result too large: %ls", arg));
|
||||
|
|
|
@ -180,8 +180,8 @@ pub fn wait(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt
|
|||
for item in &argv[optind..argc] {
|
||||
if iswnumeric(item) {
|
||||
// argument is pid
|
||||
let mpid: Result<pid_t, wutil::Error> = fish_wcstoi(item);
|
||||
if mpid.is_err() || mpid.unwrap() <= 0 {
|
||||
let mpid: pid_t = fish_wcstoi(item).unwrap_or(-1);
|
||||
if mpid <= 0 {
|
||||
streams.err.append(wgettext_fmt!(
|
||||
"%ls: '%ls' is not a valid process id\n",
|
||||
cmd,
|
||||
|
@ -189,12 +189,11 @@ pub fn wait(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt
|
|||
));
|
||||
continue;
|
||||
}
|
||||
let pid = mpid.unwrap() as pid_t;
|
||||
if !find_wait_handles(WaitHandleQuery::Pid(pid), parser, &mut wait_handles) {
|
||||
if !find_wait_handles(WaitHandleQuery::Pid(mpid), parser, &mut wait_handles) {
|
||||
streams.err.append(wgettext_fmt!(
|
||||
"%ls: Could not find a job with process id '%d'\n",
|
||||
cmd,
|
||||
pid,
|
||||
mpid,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue