Skip to content

Commit

Permalink
Auto merge of #5843 - alexcrichton:idioms-for, r=alexcrichton
Browse files Browse the repository at this point in the history
Add a `--edition-idioms` flag to `cargo fix`

This, like `--prepare-for`, will be part of the transition guide which
automatically applies the necessary lint group from the compiler to associated
code.

cc rust-lang/rust#52679
  • Loading branch information
bors committed Aug 2, 2018
2 parents e3a90f2 + 80f9d31 commit 15433e8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub fn cli() -> App {
.conflicts_with("edition")
.hidden(true),
)
.arg(
Arg::with_name("idioms")
.long("edition-idioms")
.help("Fix warnings to migrate to the idioms of an edition")
)
.arg(
Arg::with_name("allow-no-vcs")
.long("allow-no-vcs")
Expand Down Expand Up @@ -126,6 +131,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
ops::fix(&ws, &mut ops::FixOptions {
edition: args.is_present("edition"),
prepare_for: args.value_of("prepare-for"),
idioms: args.is_present("idioms"),
compile_opts: opts,
allow_dirty: args.is_present("allow-dirty"),
allow_no_vcs: args.is_present("allow-no-vcs"),
Expand Down
23 changes: 23 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
const PREPARE_FOR_ENV: &str = "__CARGO_FIX_PREPARE_FOR";
const EDITION_ENV: &str = "__CARGO_FIX_EDITION";

const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS";

pub struct FixOptions<'a> {
pub edition: bool,
pub prepare_for: Option<&'a str>,
pub idioms: bool,
pub compile_opts: CompileOptions<'a>,
pub allow_dirty: bool,
pub allow_no_vcs: bool,
Expand Down Expand Up @@ -59,6 +62,12 @@ pub fn fix(ws: &Workspace, opts: &mut FixOptions) -> CargoResult<()> {
edition.to_string(),
));
}
if opts.idioms {
opts.compile_opts.build_config.extra_rustc_env.push((
IDIOMS_ENV.to_string(),
"1".to_string(),
));
}
opts.compile_opts.build_config.cargo_as_rustc_wrapper = true;
*opts.compile_opts.build_config.rustfix_diagnostic_server.borrow_mut() =
Some(RustfixDiagnosticServer::new()?);
Expand Down Expand Up @@ -471,6 +480,7 @@ fn log_failed_fix(stderr: &[u8]) -> Result<(), Error> {
struct FixArgs {
file: Option<PathBuf>,
prepare_for_edition: PrepareFor,
idioms: bool,
enabled_edition: Option<String>,
other: Vec<OsString>,
}
Expand Down Expand Up @@ -512,6 +522,7 @@ impl FixArgs {
} else if env::var(EDITION_ENV).is_ok() {
ret.prepare_for_edition = PrepareFor::Next;
}
ret.idioms = env::var(IDIOMS_ENV).is_ok();
return ret
}

Expand All @@ -523,6 +534,12 @@ impl FixArgs {
.arg("--cap-lints=warn");
if let Some(edition) = &self.enabled_edition {
cmd.arg("--edition").arg(edition);
if self.idioms {
match &edition[..] {
"2018" => { cmd.arg("-Wrust-2018-idioms"); }
_ => {}
}
}
}
match &self.prepare_for_edition {
PrepareFor::Edition(edition) => {
Expand Down Expand Up @@ -569,6 +586,12 @@ impl FixArgs {
process::exit(1);
}

/// If we're preparing for an edition and we *don't* find the
/// `rust_2018_preview` feature, for example, in the entry point file then
/// it probably means that the edition isn't actually enabled, so we can't
/// actually fix anything.
///
/// If this is the case, issue a warning.
fn warn_if_preparing_probably_inert(&self) -> CargoResult<()> {
let edition = match &self.prepare_for_edition {
PrepareFor::Edition(s) => s,
Expand Down
33 changes: 32 additions & 1 deletion src/cargo/util/diagnostic_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub enum Message {
file: String,
edition: String,
},
IdiomEditionMismatch {
file: String,
idioms: String,
edition: Option<String>,
},
}

impl Message {
Expand Down Expand Up @@ -78,6 +83,7 @@ pub struct DiagnosticPrinter<'a> {
config: &'a Config,
preview_not_found: HashSet<String>,
edition_already_enabled: HashSet<String>,
idiom_mismatch: HashSet<String>,
}

impl<'a> DiagnosticPrinter<'a> {
Expand All @@ -86,6 +92,7 @@ impl<'a> DiagnosticPrinter<'a> {
config,
preview_not_found: HashSet::new(),
edition_already_enabled: HashSet::new(),
idiom_mismatch: HashSet::new(),
}
}

Expand Down Expand Up @@ -172,8 +179,32 @@ information about transitioning to the {0} edition see:
self.config.shell().error(&msg)?;
Ok(())
}
}
Message::IdiomEditionMismatch { file, idioms, edition } => {
// Same as above
if !self.idiom_mismatch.insert(file.clone()) {
return Ok(())
}
self.config.shell().error(&format!(
"\
cannot migrate to the idioms of the {} edition for `{}`
because it is compiled {}, which doesn't match {0}
consider migrating to the {0} edition by adding `edition = '{0}'` to
`Cargo.toml` and then rerunning this command; a more detailed transition
guide can be found at
https://rust-lang-nursery.github.io/edition-guide/editions/transitioning.html
",
idioms,
file,
match edition {
Some(s) => format!("with the {} edition", s),
None => format!("without an edition"),
},
))?;
Ok(())
}
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,62 @@ fn fix_overlapping() {
assert!(contents.contains("crate::foo::<crate::A>()"));
}

#[test]
fn fix_idioms() {
if !is_nightly() {
return
}
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ['edition']
[package]
name = 'foo'
version = '0.1.0'
edition = '2018'
"#,
)
.file(
"src/lib.rs",
r#"
use std::any::Any;
pub fn foo() {
let _x: Box<Any> = Box::new(3);
}
"#
)
.build();

let stderr = "\
[CHECKING] foo [..]
[FIXING] src/lib.rs (1 fix)
[FINISHED] [..]
";
assert_that(
p.cargo("fix --edition-idioms --allow-no-vcs")
.masquerade_as_nightly_cargo(),
execs()
.with_stderr(stderr)
.with_status(0),
);

assert!(p.read_file("src/lib.rs").contains("Box<dyn Any>"));
}

#[test]
fn idioms_2015_ok() {
let p = project()
.file("src/lib.rs", "")
.build();

assert_that(
p.cargo("fix --edition-idioms --allow-no-vcs")
.masquerade_as_nightly_cargo(),
execs().with_status(0),
);
}

#[test]
fn both_edition_migrate_flags() {
if !is_nightly() {
Expand Down

0 comments on commit 15433e8

Please sign in to comment.