Skip to content

Commit 1b76b42

Browse files
committed
simplify some code using early returns
1 parent c9b395b commit 1b76b42

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

crates/ra_env/src/lib.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,24 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
3333
)))
3434
}
3535
} else {
36-
let final_path: Option<String> = if is_valid_executable(executable_name) {
37-
Some(executable_name.to_owned())
38-
} else {
39-
if let Some(mut path) = dirs::home_dir() {
40-
path.push(".cargo");
41-
path.push("bin");
42-
path.push(executable_name);
43-
if is_valid_executable(&path) {
44-
Some(path.into_os_string().into_string().expect("Invalid Unicode in path"))
45-
} else {
46-
None
47-
}
48-
} else {
49-
None
36+
if is_valid_executable(executable_name) {
37+
return Ok(executable_name.to_owned());
38+
}
39+
if let Some(mut path) = dirs::home_dir() {
40+
path.push(".cargo");
41+
path.push("bin");
42+
path.push(executable_name);
43+
if is_valid_executable(&path) {
44+
return Ok(path.into_os_string().into_string().expect("Invalid Unicode in path"));
5045
}
51-
};
52-
final_path.ok_or(
53-
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
54-
// for VSCode, even if they are set correctly in a terminal.
55-
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
56-
// to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
57-
// but launching VSCode from Dock does not inherit environment variables from a terminal.
58-
// For more discussion, see #3118.
59-
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))
60-
)
46+
}
47+
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly
48+
// for VSCode, even if they are set correctly in a terminal.
49+
// On macOS in particular, launching VSCode from terminal with `code <dirname>` causes VSCode
50+
// to inherit environment variables including $PATH, $CARGO, $RUSTC, etc from that terminal;
51+
// but launching VSCode from Dock does not inherit environment variables from a terminal.
52+
// For more discussion, see #3118.
53+
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)))
6154
}
6255
}
6356

0 commit comments

Comments
 (0)