Skip to content

Commit c227565

Browse files
committed
Auto merge of #9846 - ehuss:fix-only-edition-lints, r=alexcrichton
Change `cargo fix --edition` to only fix edition lints. This changes it so that `cargo fix --edition` will only fix edition lints. The reason for this is that sometimes non-edition lints get in the way, and make suggestions that can cause failures. An example is a user that only ever runs `cargo test` or `cargo check --profile=test` locally, and doesn't realize there are problems with running without `cfg(test)` such as unused warnings. This works by using `--cap-lints=allow` along with `--force-warn` which takes precedence over `cap-lints`. This only works on nightly since `--force-warn` is still unstable. I will update this as part of #9800. Closes #5738
2 parents 46fa867 + 46450d6 commit c227565

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/cargo/ops/fix.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,17 @@ impl FixArgs {
824824

825825
fn apply(&self, cmd: &mut Command, config: &Config) {
826826
cmd.arg(&self.file);
827-
cmd.args(&self.other).arg("--cap-lints=warn");
827+
cmd.args(&self.other);
828+
if self.prepare_for_edition.is_some() && config.nightly_features_allowed {
829+
// When migrating an edition, we don't want to fix other lints as
830+
// they can sometimes add suggestions that fail to apply, causing
831+
// the entire migration to fail. But those lints aren't needed to
832+
// migrate.
833+
cmd.arg("--cap-lints=allow");
834+
} else {
835+
// This allows `cargo fix` to work even if the crate has #[deny(warnings)].
836+
cmd.arg("--cap-lints=warn");
837+
}
828838
if let Some(edition) = self.enabled_edition {
829839
cmd.arg("--edition").arg(edition.to_string());
830840
if self.idioms && edition.supports_idiom_lint() {

tests/testsuite/fix.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,3 +1745,50 @@ fn fix_with_run_cargo_in_proc_macros() {
17451745
.with_stderr_does_not_contain("error: could not find .rs file in rustc args")
17461746
.run();
17471747
}
1748+
1749+
#[cargo_test]
1750+
fn non_edition_lint_migration() {
1751+
// Migrating to a new edition where a non-edition lint causes problems.
1752+
if !is_nightly() {
1753+
// Remove once force-warn hits stable.
1754+
return;
1755+
}
1756+
let p = project()
1757+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
1758+
.file(
1759+
"src/lib.rs",
1760+
r#"
1761+
// This is only used in a test.
1762+
// To be correct, this should be gated on #[cfg(test)], but
1763+
// sometimes people don't do that. If the unused_imports
1764+
// lint removes this, then the unittest will fail to compile.
1765+
use std::str::from_utf8;
1766+
1767+
pub mod foo {
1768+
pub const FOO: &[u8] = &[102, 111, 111];
1769+
}
1770+
1771+
#[test]
1772+
fn example() {
1773+
assert_eq!(
1774+
from_utf8(::foo::FOO), Ok("foo")
1775+
);
1776+
}
1777+
"#,
1778+
)
1779+
.build();
1780+
// Check that it complains about an unused import.
1781+
p.cargo("check --lib")
1782+
.with_stderr_contains("[..]unused_imports[..]")
1783+
.with_stderr_contains("[..]std::str::from_utf8[..]")
1784+
.run();
1785+
p.cargo("fix --edition --allow-no-vcs")
1786+
// Remove once --force-warn is stabilized
1787+
.masquerade_as_nightly_cargo()
1788+
.run();
1789+
let contents = p.read_file("src/lib.rs");
1790+
// Check it does not remove the "unused" import.
1791+
assert!(contents.contains("use std::str::from_utf8;"));
1792+
// Check that it made the edition migration.
1793+
assert!(contents.contains("from_utf8(crate::foo::FOO)"));
1794+
}

0 commit comments

Comments
 (0)