Add a migrating message for `cargo fix --edition`.

This helps indicate which edition you are moving from and to.
This commit is contained in:
Eric Huss 2021-02-17 21:36:28 -08:00
parent 820537c706
commit 3f2f7e30ff
4 changed files with 44 additions and 24 deletions

View File

@ -1535,6 +1535,7 @@ fn substitute_macros(input: &str) -> String {
("[LOGOUT]", " Logout"),
("[YANK]", " Yank"),
("[OWNER]", " Owner"),
("[MIGRATING]", " Migrating"),
];
let mut result = input.to_owned();
for &(pat, subst) in &macros {

View File

@ -301,7 +301,7 @@ fn rustfix_crate(
filename: &Path,
args: &FixArgs,
) -> Result<FixedCrate, Error> {
args.verify_not_preparing_for_enabled_edition()?;
args.check_edition_and_send_status()?;
// First up, we want to make sure that each crate is only checked by one
// process at a time. If two invocations concurrently check a crate then
@ -685,15 +685,10 @@ impl FixArgs {
}
}
/// Verifies that we're not both preparing for an enabled edition and enabling
/// the edition.
///
/// This indicates that `cargo fix --prepare-for` is being executed out of
/// order with enabling the edition itself, meaning that we wouldn't
/// actually be able to fix anything! If it looks like this is happening
/// then yield an error to the user, indicating that this is happening.
fn verify_not_preparing_for_enabled_edition(&self) -> CargoResult<()> {
let edition = match self.prepare_for_edition {
/// Validates the edition, and sends a message indicating what is being
/// done.
fn check_edition_and_send_status(&self) -> CargoResult<()> {
let to_edition = match self.prepare_for_edition {
Some(s) => s,
None => {
return Message::Fixing {
@ -702,20 +697,21 @@ impl FixArgs {
.post();
}
};
let enabled = match self.enabled_edition {
Some(s) => s,
None => return Ok(()),
};
if edition != enabled {
return Ok(());
let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015);
if from_edition == to_edition {
Message::EditionAlreadyEnabled {
file: self.file.display().to_string(),
edition: to_edition,
}
.post()?;
process::exit(1);
} else {
Message::Migrating {
file: self.file.display().to_string(),
from_edition,
to_edition,
}
.post()
}
Message::EditionAlreadyEnabled {
file: self.file.display().to_string(),
edition,
}
.post()?;
process::exit(1);
}
}

View File

@ -31,6 +31,11 @@ const PLEASE_REPORT_THIS_BUG: &str =
#[derive(Deserialize, Serialize, Hash, Eq, PartialEq, Clone)]
pub enum Message {
Migrating {
file: String,
from_edition: Edition,
to_edition: Edition,
},
Fixing {
file: String,
},
@ -92,6 +97,19 @@ impl<'a> DiagnosticPrinter<'a> {
pub fn print(&mut self, msg: &Message) -> CargoResult<()> {
match msg {
Message::Migrating {
file,
from_edition,
to_edition,
} => {
if !self.dedupe.insert(msg.clone()) {
return Ok(());
}
self.config.shell().status(
"Migrating",
&format!("{} from {} edition to {}", file, from_edition, to_edition),
)
}
Message::Fixing { file } => self
.config
.shell()

View File

@ -286,6 +286,7 @@ fn prepare_for_2018() {
let stderr = "\
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (2 fixes)
[FINISHED] [..]
";
@ -324,6 +325,7 @@ fn local_paths() {
.with_stderr(
"\
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (1 fix)
[FINISHED] [..]
",
@ -409,6 +411,7 @@ fn specify_rustflags() {
.with_stderr(
"\
[CHECKING] foo v0.0.1 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (1 fix)
[FINISHED] [..]
",
@ -842,6 +845,7 @@ fn fix_overlapping() {
.with_stderr(
"\
[CHECKING] foo [..]
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FIXED] src/lib.rs (2 fixes)
[FINISHED] dev [..]
",
@ -1164,6 +1168,7 @@ fn only_warn_for_relevant_crates() {
"\
[CHECKING] a v0.1.0 ([..])
[CHECKING] foo v0.1.0 ([..])
[MIGRATING] src/lib.rs from 2015 edition to 2018
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)