diff --git a/cargo-fix/src/cli.rs b/cargo-fix/src/cli.rs index b8dbbc7..c8b2260 100644 --- a/cargo-fix/src/cli.rs +++ b/cargo-fix/src/cli.rs @@ -33,6 +33,13 @@ pub fn run() -> Result<(), Error> { Arg::with_name("broken-code") .long("broken-code") .help("Fix code even if it already has compiler errors"), + ) + .arg( + Arg::with_name("edition") + .long("prepare-for") + .help("Fix warnings in preparation of an edition upgrade") + .takes_value(true) + .possible_values(&["2018"]), ), ) .get_matches(); @@ -77,6 +84,15 @@ pub fn run() -> Result<(), Error> { cmd.env("RUSTC_ORIGINAL", rustc); } + // Trigger edition-upgrade mode. Currently only supports the 2018 edition. + info!("edition upgrade? {:?}", matches.value_of("edition")); + if let Some("2018") = matches.value_of("edition") { + info!("edition upgrade!"); + let mut rustc_flags = env::var_os("RUSTFLAGS").unwrap_or_else(|| "".into()); + rustc_flags.push("-W rust-2018-breakage"); + cmd.env("RUSTFLAGS", &rustc_flags); + } + // An now execute all of Cargo! This'll fix everything along the way. // // TODO: we probably want to do something fancy here like collect results diff --git a/cargo-fix/tests/all/edition_upgrade.rs b/cargo-fix/tests/all/edition_upgrade.rs new file mode 100644 index 0000000..66ea90c --- /dev/null +++ b/cargo-fix/tests/all/edition_upgrade.rs @@ -0,0 +1,46 @@ +//! Test that we can use cargo-fix to upgrade our code to work with the 2018 +//! edition. +//! +//! We'll trigger the `absolute_path_starting_with_module` lint which should +//! transform a `use ::foo;` where `foo` is local to `use crate::foo;`. + +use super::project; + +#[test] +fn prepare_for_2018() { + let p = project() + .file( + "src/lib.rs", + r#" + #![allow(unused)] + #![feature(rust_2018_preview)] + + mod foo { + pub const FOO: &str = "fooo"; + } + + mod bar { + use ::foo::FOO; + } + + fn main() { + let x = ::foo::FOO; + } + "#, + ) + .build(); + + let stderr = "\ +[CHECKING] foo v0.1.0 (CWD) +[FIXING] src/lib.rs (2 fixes) +[FINISHED] dev [unoptimized + debuginfo] +"; + p.expect_cmd("cargo-fix fix --prepare-for 2018") + .stdout("") + .stderr(stderr) + .run(); + + println!("{}", p.read("src/lib.rs")); + assert!(p.read("src/lib.rs").contains("use crate::foo::FOO;")); + assert!(p.read("src/lib.rs").contains("let x = crate::foo::FOO;")); +} diff --git a/cargo-fix/tests/all/main.rs b/cargo-fix/tests/all/main.rs index 62f03da..5f90b3d 100644 --- a/cargo-fix/tests/all/main.rs +++ b/cargo-fix/tests/all/main.rs @@ -318,6 +318,7 @@ fn diff(expected: &str, actual: &str) { mod broken_build; mod broken_lints; mod dependencies; +mod edition_upgrade; mod smoke; mod subtargets; mod warnings;