simplify some code using early returns

This commit is contained in:
Craig Disselkoen 2020-05-05 16:09:39 -07:00
parent c9b395be2b
commit 1b76b4281e

View file

@ -33,31 +33,24 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
))) )))
} }
} else { } else {
let final_path: Option<String> = if is_valid_executable(executable_name) { if is_valid_executable(executable_name) {
Some(executable_name.to_owned()) return Ok(executable_name.to_owned());
} else { }
if let Some(mut path) = dirs::home_dir() { if let Some(mut path) = dirs::home_dir() {
path.push(".cargo"); path.push(".cargo");
path.push("bin"); path.push("bin");
path.push(executable_name); path.push(executable_name);
if is_valid_executable(&path) { if is_valid_executable(&path) {
Some(path.into_os_string().into_string().expect("Invalid Unicode in path")) return Ok(path.into_os_string().into_string().expect("Invalid Unicode in path"));
} else {
None
}
} else {
None
} }
}; }
final_path.ok_or( // This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly // for VSCode, even if they are set correctly in a terminal.
// for VSCode, even if they are set correctly in a terminal. // On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode // to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
// to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal; // but launching VSCode from Dock does not inherit environment variables from a terminal.
// but launching VSCode from Dock does not inherit environment variables from a terminal. // For more discussion, see #3118.
// For more discussion, see #3118. Err(Error::msg(format!("Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.", executable_name, executable_name, env_var)))
Error::msg(format!("Failed to find `{}` executable. Make sure `{}` is in `$PATH`, or set `${}` to point to a valid executable.", executable_name, executable_name, env_var))
)
} }
} }