Skip to content

Commit 6026fc3

Browse files
committed
Auto merge of #6247 - alexcrichton:fix-less, r=dwijnand
Don't turn on edition lints for unfixed crates Currently Cargo runs the risk of turning on the edition lints for crates which `cargo fix` isn't actually fixing, which means you'll get a huge deluge of lints that would otherwise be automatically fixable! Fix this situation by only enabling lints in the same cases that we're actually applying fixes. Closes rust-lang-nursery/rustfix#150
2 parents bd32ba7 + 4f784a1 commit 6026fc3

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/cargo/ops/fix.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
165165
// not the best heuristic but matches what Cargo does today at least.
166166
let mut fixes = FixedCrate::default();
167167
if let Some(path) = &args.file {
168-
if env::var("CARGO_PRIMARY_PACKAGE").is_ok() {
168+
if args.primary_package {
169169
trace!("start rustfixing {:?}", path);
170170
fixes = rustfix_crate(&lock_addr, rustc.as_ref(), path, &args)?;
171171
}
@@ -503,6 +503,7 @@ struct FixArgs {
503503
idioms: bool,
504504
enabled_edition: Option<String>,
505505
other: Vec<OsString>,
506+
primary_package: bool,
506507
}
507508

508509
enum PrepareFor {
@@ -543,6 +544,7 @@ impl FixArgs {
543544
ret.prepare_for_edition = PrepareFor::Next;
544545
}
545546
ret.idioms = env::var(IDIOMS_ENV).is_ok();
547+
ret.primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
546548
ret
547549
}
548550

@@ -554,12 +556,14 @@ impl FixArgs {
554556
.arg("--cap-lints=warn");
555557
if let Some(edition) = &self.enabled_edition {
556558
cmd.arg("--edition").arg(edition);
557-
if self.idioms {
559+
if self.idioms && self.primary_package {
558560
if edition == "2018" { cmd.arg("-Wrust-2018-idioms"); }
559561
}
560562
}
561-
if let Some(edition) = self.prepare_for_edition_resolve() {
562-
cmd.arg("-W").arg(format!("rust-{}-compatibility", edition));
563+
if self.primary_package {
564+
if let Some(edition) = self.prepare_for_edition_resolve() {
565+
cmd.arg("-W").arg(format!("rust-{}-compatibility", edition));
566+
}
563567
}
564568
}
565569

tests/testsuite/fix.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,47 @@ fn does_not_crash_with_rustc_wrapper() {
10721072
.env("RUSTC_WRAPPER", "/usr/bin/env")
10731073
.run();
10741074
}
1075+
1076+
#[test]
1077+
fn only_warn_for_relevant_crates() {
1078+
let p = project()
1079+
.file(
1080+
"Cargo.toml",
1081+
r#"
1082+
[package]
1083+
name = "foo"
1084+
version = "0.1.0"
1085+
1086+
[dependencies]
1087+
a = { path = 'a' }
1088+
"#,
1089+
)
1090+
.file("src/lib.rs", "")
1091+
.file(
1092+
"a/Cargo.toml",
1093+
r#"
1094+
[package]
1095+
name = "a"
1096+
version = "0.1.0"
1097+
"#,
1098+
)
1099+
.file(
1100+
"a/src/lib.rs",
1101+
"
1102+
pub fn foo() {}
1103+
pub mod bar {
1104+
use foo;
1105+
pub fn baz() { foo() }
1106+
}
1107+
",
1108+
)
1109+
.build();
1110+
1111+
p.cargo("fix --allow-no-vcs --edition")
1112+
.with_stderr("\
1113+
[CHECKING] a v0.1.0 ([..])
1114+
[CHECKING] foo v0.1.0 ([..])
1115+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1116+
")
1117+
.run();
1118+
}

0 commit comments

Comments
 (0)