diff --git a/src/bin/cargo/commands/check.rs b/src/bin/cargo/commands/check.rs index 74c1a688927..819e1346893 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -27,6 +27,7 @@ pub fn cli() -> App { "Check all targets", ) .arg_release("Check artifacts in release mode, with optimizations") + .arg_force_rebuild("Force rebuild of the selected target(s)") .arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE")) .arg_features() .arg_target_triple("Check for the target triple") diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index a5e42dbe438..325d4525ad8 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -185,6 +185,10 @@ pub trait AppExt: Sized { fn arg_dry_run(self, dry_run: &'static str) -> Self { self._arg(opt("dry-run", dry_run)) } + + fn arg_force_rebuild(self, force_rebuild: &'static str) -> Self { + self._arg(opt("force-rebuild", force_rebuild)) + } } impl AppExt for App { @@ -362,6 +366,12 @@ pub trait ArgMatchesExt { let mut build_config = BuildConfig::new(config, self.jobs()?, &self.target(), mode)?; build_config.message_format = message_format.unwrap_or(MessageFormat::Human); build_config.release = self._is_present("release"); + build_config.force_rebuild = self._is_present("force-rebuild"); + if build_config.force_rebuild && !config.cli_unstable().unstable_options { + Err(failure::format_err!( + "`--force-rebuild` flag is unstable, pass `-Z unstable-options` to enable it" + ))?; + }; build_config.build_plan = self._is_present("build-plan"); if build_config.build_plan { config diff --git a/tests/testsuite/check.rs b/tests/testsuite/check.rs index a4eab3b2229..2d83f63e85a 100644 --- a/tests/testsuite/check.rs +++ b/tests/testsuite/check.rs @@ -194,6 +194,32 @@ fn build_check() { foo.cargo("check -v").run(); } +// Checks that --force-rebuild displays warnings even if the project has not changed +// since the last check/build +#[test] +fn force_rebuild_displays_error() { + let foo = project() + .file( + "src/main.rs", + "use std::default::Default; fn main() {}", + ) + .build(); + + foo.cargo("check") + .with_stderr_contains("[..]warning: unused import[..]") + .run(); + + // for now this requires the unstable feature flag, so expect an error here + let output = foo.cargo("check --force-rebuild") + .exec_with_output(); + assert!(output.is_err()); + + foo.cargo("check -Z unstable-options --force-rebuild") + .masquerade_as_nightly_cargo() // remove this when `-Z unstable-options` is no longer required + .with_stderr_contains("[..]warning: unused import[..]") + .run(); +} + // Checks that where a project has both a lib and a bin, the lib is only checked // not built. #[cargo_test]