agent/rustjail: Clean up error path in execute_hook()s async task

Clippy (in Rust 1.51 at least) has some complaints about this closure
inside execute_hook() because it uses explicit returns in some places
where it doesn't need them, because they're the last expression in the
function.

That isn't necessarily obvious from a glance, but we can make clippy happy
and also make things a little clearer: first we replace a somewhat verbose
'match' using Option::ok_or_else(), then rearrange the remaining code to
put all the error path first with an explicit return then the "happy" path
as the stright line exit with an implicit return.

fixes #1611

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2021-04-16 16:41:13 +10:00
parent 6ce1e56d20
commit 75eca6d56f

View File

@ -1528,28 +1528,23 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
match child.wait().await {
Ok(exit) => {
let code = match exit.code() {
Some(c) => c,
None => {
return Err(anyhow!("hook exit status has no status code"));
}
};
let code = exit
.code()
.ok_or_else(|| anyhow!("hook exit status has no status code"))?;
if code == 0 {
debug!(logger, "hook {} exit status is 0", &path);
return Ok(());
} else {
if code != 0 {
error!(logger, "hook {} exit status is {}", &path, code);
return Err(anyhow!(nix::Error::from_errno(Errno::UnknownErrno)));
}
debug!(logger, "hook {} exit status is 0", &path);
Ok(())
}
Err(e) => {
return Err(anyhow!(
"wait child error: {} {}",
e,
e.raw_os_error().unwrap()
));
}
Err(e) => Err(anyhow!(
"wait child error: {} {}",
e,
e.raw_os_error().unwrap()
)),
}
});