From 061edfe1fa02a194c94d3aadc3eb4ac579ba3bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 4 Jul 2024 15:26:09 +0200 Subject: [PATCH] Add `#[must_use]` attribute to several command-related methods This should make it harder to accidentally forget to use results of methods on `BootstrapCommand` and `CommandStatus`. --- src/bootstrap/src/utils/exec.rs | 12 ++++++++++++ src/bootstrap/src/utils/helpers.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/utils/exec.rs b/src/bootstrap/src/utils/exec.rs index b6872d1c99f..ba963f52dc2 100644 --- a/src/bootstrap/src/utils/exec.rs +++ b/src/bootstrap/src/utils/exec.rs @@ -109,14 +109,17 @@ impl BootstrapCommand { self } + #[must_use] pub fn delay_failure(self) -> Self { Self { failure_behavior: BehaviorOnFailure::DelayFail, ..self } } + #[must_use] pub fn fail_fast(self) -> Self { Self { failure_behavior: BehaviorOnFailure::Exit, ..self } } + #[must_use] pub fn allow_failure(self) -> Self { Self { failure_behavior: BehaviorOnFailure::Ignore, ..self } } @@ -127,11 +130,13 @@ impl BootstrapCommand { } /// Capture all output of the command, do not print it. + #[must_use] pub fn capture(self) -> Self { Self { stdout: OutputMode::Capture, stderr: OutputMode::Capture, ..self } } /// Capture stdout of the command, do not print it. + #[must_use] pub fn capture_stdout(self) -> Self { Self { stdout: OutputMode::Capture, ..self } } @@ -178,10 +183,12 @@ pub struct CommandOutput { } impl CommandOutput { + #[must_use] pub fn did_not_start() -> Self { Self { status: CommandStatus::DidNotStart, stdout: vec![], stderr: vec![] } } + #[must_use] pub fn is_success(&self) -> bool { match self.status { CommandStatus::Finished(status) => status.success(), @@ -189,10 +196,12 @@ impl CommandOutput { } } + #[must_use] pub fn is_failure(&self) -> bool { !self.is_success() } + #[must_use] pub fn status(&self) -> Option { match self.status { CommandStatus::Finished(status) => Some(status), @@ -200,14 +209,17 @@ impl CommandOutput { } } + #[must_use] pub fn stdout(&self) -> String { String::from_utf8(self.stdout.clone()).expect("Cannot parse process stdout as UTF-8") } + #[must_use] pub fn stdout_if_ok(&self) -> Option { if self.is_success() { Some(self.stdout()) } else { None } } + #[must_use] pub fn stderr(&self) -> String { String::from_utf8(self.stderr.clone()).expect("Cannot parse process stderr as UTF-8") } diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 8ce0f59399f..5dd3ba96786 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -499,7 +499,7 @@ pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String { /// Whenever a git invocation is needed, this function should be preferred over /// manually building a git `BootstrapCommand`. This approach allows us to manage /// bootstrap-specific needs/hacks from a single source, rather than applying them on next to every -/// `BootstrapCommand::new("git")`, which is painful to ensure that the required change is applied +/// git command creation, which is painful to ensure that the required change is applied /// on each one of them correctly. pub fn git(source_dir: Option<&Path>) -> BootstrapCommand { let mut git = command("git");