diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index 7197cdbad35..d503f43ddcc 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -30,6 +30,7 @@ pub fn cli() -> Command { ) .arg_features() .arg_release("Build artifacts in release mode, with optimizations") + .arg_redundant_default_mode("debug", "build", "release") .arg_profile("Build artifacts with the specified profile") .arg_parallel() .arg_target_triple("Build for the target triple") diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 010a4f48385..2e5163bdc94 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -83,6 +83,7 @@ pub fn cli() -> Command { "debug", "Build in debug mode (with the 'dev' profile) instead of release mode", )) + .arg_redundant_default_mode("release", "install", "debug") .arg_profile("Install artifacts with the specified profile") .arg_target_triple("Build for the target triple") .arg_target_dir() diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index c8dc4df87c1..dcfabb3c259 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -112,6 +112,17 @@ pub trait CommandExt: Sized { self._arg(flag("keep-going", "").value_parser(value_parser).hide(true)) } + fn arg_redundant_default_mode( + self, + default_mode: &'static str, + command: &'static str, + supported_mode: &'static str, + ) -> Self { + let msg = format!("`--{default_mode}` is the default for `cargo {command}`; instead `--{supported_mode}` is supported"); + let value_parser = UnknownArgumentValueParser::suggest(msg); + self._arg(flag(default_mode, "").value_parser(value_parser).hide(true)) + } + fn arg_targets_all( self, lib: &'static str, @@ -486,7 +497,7 @@ Run `{cmd}` to see possible targets." (Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc) // `cargo fix` and `cargo check` has legacy handling of this profile name | (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => { - if self.flag("release") { + if self.maybe_flag("release") { config.shell().warn( "the `--release` flag should not be specified with the `--profile` flag\n\ The `--release` flag will be ignored.\n\ @@ -510,7 +521,11 @@ Run `{cmd}` to see possible targets." ) }; - let name = match (self.flag("release"), self.flag("debug"), specified_profile) { + let name = match ( + self.maybe_flag("release"), + self.maybe_flag("debug"), + specified_profile, + ) { (false, false, None) => default, (true, _, None | Some("release")) => "release", (true, _, Some(name)) => return Err(conflict("release", "release", name)), diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 3c650ca5213..1afa83918f0 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -135,6 +135,29 @@ fn incremental_config() { .run(); } +#[cargo_test] +fn cargo_compile_with_redundant_default_mode() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/foo.rs", &main_file(r#""i am foo""#, &[])) + .build(); + + p.cargo("build --debug") + .with_stderr( + "\ +error: unexpected argument '--debug' found + + tip: `--debug` is the default for `cargo build`; instead `--release` is supported + +Usage: cargo[EXE] build [OPTIONS] + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +} + #[cargo_test] fn cargo_compile_with_workspace_excluded() { let p = project().file("src/main.rs", "fn main() {}").build(); diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index c7658037271..0a3670e6c8b 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2443,3 +2443,23 @@ fn ambiguous_registry_vs_local_package() { .run(); assert_has_installed_exe(cargo_home(), "foo"); } + +#[cargo_test] +fn install_with_redundant_default_mode() { + pkg("foo", "0.0.1"); + + cargo_process("install foo --release") + .with_stderr( + "\ +error: unexpected argument '--release' found + + tip: `--release` is the default for `cargo install`; instead `--debug` is supported + +Usage: cargo[EXE] install [OPTIONS] [crate]... + +For more information, try '--help'. +", + ) + .with_status(1) + .run(); +}