Skip to content

Commit

Permalink
Don't turn on edition lints for unfixed crates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Nov 2, 2018
1 parent efb7972 commit c61cc58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
// not the best heuristic but matches what Cargo does today at least.
let mut fixes = FixedCrate::default();
if let Some(path) = &args.file {
if env::var("CARGO_PRIMARY_PACKAGE").is_ok() {
if args.primary_package {
trace!("start rustfixing {:?}", path);
fixes = rustfix_crate(&lock_addr, rustc.as_ref(), path, &args)?;
}
Expand Down Expand Up @@ -503,6 +503,7 @@ struct FixArgs {
idioms: bool,
enabled_edition: Option<String>,
other: Vec<OsString>,
primary_package: bool,
}

enum PrepareFor {
Expand Down Expand Up @@ -543,6 +544,7 @@ impl FixArgs {
ret.prepare_for_edition = PrepareFor::Next;
}
ret.idioms = env::var(IDIOMS_ENV).is_ok();
ret.primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
ret
}

Expand All @@ -554,12 +556,14 @@ impl FixArgs {
.arg("--cap-lints=warn");
if let Some(edition) = &self.enabled_edition {
cmd.arg("--edition").arg(edition);
if self.idioms {
if self.idioms && self.primary_package {
if edition == "2018" { cmd.arg("-Wrust-2018-idioms"); }
}
}
if let Some(edition) = self.prepare_for_edition_resolve() {
cmd.arg("-W").arg(format!("rust-{}-compatibility", edition));
if self.primary_package {
if let Some(edition) = self.prepare_for_edition_resolve() {
cmd.arg("-W").arg(format!("rust-{}-compatibility", edition));
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,47 @@ fn does_not_crash_with_rustc_wrapper() {
.env("RUSTC_WRAPPER", "/usr/bin/env")
.run();
}

#[test]
fn only_warn_for_relevant_crates() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
a = { path = 'a' }
"#,
)
.file("src/lib.rs", "")
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
"#,
)
.file(
"a/src/lib.rs",
"
pub fn foo() {}
pub mod bar {
use foo;
pub fn baz() { foo() }
}
",
)
.build();

p.cargo("fix --allow-no-vcs --edition")
.with_stderr("\
[CHECKING] a v0.1.0 ([..])
[CHECKING] foo v0.1.0 ([..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
")
.run();
}

0 comments on commit c61cc58

Please sign in to comment.