Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit cea8fd1

Browse files
committed
WIP: Add --prepare-for flag
This requires #![feature(crate_in_paths)] #![deny(absolute_path_starting_with_module)] on `rustc 1.27.0-nightly (8ff4b4206 2018-05-08)`, so we're not really there yet.
1 parent b5e4e61 commit cea8fd1

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

cargo-fix/src/cli.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ pub fn run() -> Result<(), Error> {
3333
Arg::with_name("broken-code")
3434
.long("broken-code")
3535
.help("Fix code even if it already has compiler errors"),
36+
)
37+
.arg(
38+
Arg::with_name("edition")
39+
.long("prepare-for")
40+
.help("Fix warnigns in preparation of an edition upgrade")
41+
.takes_value(true)
42+
.possible_values(&["2018"]),
3643
),
3744
)
3845
.get_matches();
@@ -77,6 +84,15 @@ pub fn run() -> Result<(), Error> {
7784
cmd.env("RUSTC_ORIGINAL", rustc);
7885
}
7986

87+
// Trigger edition-upgrade mode. Currently only supports the 2018 edition.
88+
info!("edition upgrade? {:?}", matches.value_of("edition"));
89+
if let Some("2018") = matches.value_of("edition") {
90+
info!("edition upgrade!");
91+
let mut rustc_flags = env::var_os("RUSTC_FLAGS").unwrap_or_else(|| "".into());
92+
rustc_flags.push("-A warnings -W rust_2018_breakage");
93+
cmd.env("RUSTC_FLAGS", &rustc_flags);
94+
}
95+
8096
// An now execute all of Cargo! This'll fix everything along the way.
8197
//
8298
// TODO: we probably want to do something fancy here like collect results
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Test that we can use cargo-fix to upgrade our code to work with the 2018
2+
//! edition.
3+
//!
4+
//! We'll trigger the `absolute_path_starting_with_module` lint which should
5+
//! transform a `use ::foo;` where `foo` is local to `use crate::foo;`.
6+
7+
use super::project;
8+
9+
#[test]
10+
fn prepare_for_2018() {
11+
let p = project()
12+
.file(
13+
"src/lib.rs",
14+
r#"
15+
#![allow(unused)]
16+
#![feature(crate_in_paths)]
17+
#![warn(absolute_path_starting_with_module)]
18+
19+
mod foo {
20+
pub const FOO: &str = "fooo";
21+
}
22+
23+
mod bar {
24+
use ::foo::FOO;
25+
}
26+
27+
fn main() {
28+
let x = ::foo::FOO;
29+
}
30+
"#,
31+
)
32+
.build();
33+
34+
let stderr = "\
35+
[CHECKING] foo v0.1.0 (CWD)
36+
[FIXING] src/lib.rs (2 fixes)
37+
[FINISHED] dev [unoptimized + debuginfo]
38+
";
39+
p.expect_cmd("cargo-fix fix --prepare-for 2018")
40+
.stdout("")
41+
.stderr(stderr)
42+
.run();
43+
44+
println!("{}", p.read("src/lib.rs"));
45+
assert!(p.read("src/lib.rs").contains("use crate::foo::FOO;"));
46+
assert!(p.read("src/lib.rs").contains("let x = crate::foo::FOO;"));
47+
}

cargo-fix/tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ fn diff(expected: &str, actual: &str) {
318318
mod broken_build;
319319
mod broken_lints;
320320
mod dependencies;
321+
mod edition_upgrade;
321322
mod smoke;
322323
mod subtargets;
323324
mod warnings;

0 commit comments

Comments
 (0)