Skip to content

Commit a92432f

Browse files
committed
toolchain: hoist binary name conditionals out of fallback functions
1 parent 4c5de18 commit a92432f

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/toolchain.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,35 +332,36 @@ impl<'a> Toolchain<'a> {
332332
pub(crate) fn command(&self, binary: &str) -> anyhow::Result<Command> {
333333
// Should push the cargo fallback into a custom toolchain type? And then
334334
// perhaps a trait that create command layers on?
335-
if let Some(cmd) = self.maybe_do_cargo_fallback(binary)? {
336-
info!("`cargo` is unavailable for the active toolchain");
337-
info!("falling back to {:?}", cmd.get_program());
338-
return Ok(cmd);
339-
} else if let Some(cmd) = self.maybe_do_rust_analyzer_fallback(binary)? {
340-
info!("`rust-analyzer` is unavailable for the active toolchain");
341-
info!("falling back to {:?}", cmd.get_program());
342-
return Ok(cmd);
335+
if let "cargo" | "cargo.exe" = binary {
336+
if let Some(cmd) = self.maybe_do_cargo_fallback()? {
337+
info!("`cargo` is unavailable for the active toolchain");
338+
info!("falling back to {:?}", cmd.get_program());
339+
return Ok(cmd);
340+
}
341+
} else if let "rust-analyzer" | "rust-analyzer.exe" = binary {
342+
if let Some(cmd) = self.maybe_do_rust_analyzer_fallback(binary)? {
343+
info!("`rust-analyzer` is unavailable for the active toolchain");
344+
info!("falling back to {:?}", cmd.get_program());
345+
return Ok(cmd);
346+
}
343347
}
344348

345349
self.create_command(binary)
346350
}
347351

348352
// Custom toolchains don't have cargo, so here we detect that situation and
349353
// try to find a different cargo.
350-
fn maybe_do_cargo_fallback(&self, binary: &str) -> anyhow::Result<Option<Command>> {
354+
fn maybe_do_cargo_fallback(&self) -> anyhow::Result<Option<Command>> {
351355
if let LocalToolchainName::Named(ToolchainName::Official(_)) = self.name() {
352356
return Ok(None);
353-
} else if binary != "cargo" && binary != "cargo.exe" {
354-
return Ok(None);
355357
}
356358

357-
let cargo_path = self.binary_file("cargo");
358-
359359
// breadcrumb in case of regression: we used to get the cargo path and
360360
// cargo.exe path separately, not using the binary_file helper. This may
361361
// matter if calling a binary with some personality that allows .exe and
362362
// not .exe to coexist (e.g. wine) - but that's not something we aim to
363363
// support : the host should always be correct.
364+
let cargo_path = self.binary_file("cargo");
364365
if cargo_path.exists() {
365366
return Ok(None);
366367
}
@@ -391,9 +392,7 @@ impl<'a> Toolchain<'a> {
391392
/// - <https://github.com/rust-lang/rustup/issues/3299>
392393
/// - <https://github.com/rust-lang/rustup/issues/3846>
393394
fn maybe_do_rust_analyzer_fallback(&self, binary: &str) -> anyhow::Result<Option<Command>> {
394-
if binary != "rust-analyzer" && binary != "rust-analyzer.exe"
395-
|| self.binary_file("rust-analyzer").exists()
396-
{
395+
if self.binary_file("rust-analyzer").exists() {
397396
return Ok(None);
398397
}
399398

0 commit comments

Comments
 (0)