From 75eca6d56f1540b93452c0417547db57291178e6 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 16 Apr 2021 16:41:13 +1000 Subject: [PATCH] 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 --- src/agent/rustjail/src/container.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/agent/rustjail/src/container.rs b/src/agent/rustjail/src/container.rs index 25be015e0..ede65926a 100644 --- a/src/agent/rustjail/src/container.rs +++ b/src/agent/rustjail/src/container.rs @@ -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() + )), } });