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

Commit 040686f

Browse files
committed
Custom warning when rustfix replacing fails
When rustfix fails to replace a chunk of code, we now display a similar warning than when we "fixed" the code so that it no longer compiles. Previously, when rustfix fails to apply suggestions to a file, we bailed out of the whole loop over all the files in the current target. Instead, we'll now log a nice error message. (I'm not sure if that is a good idea but we will revert non-compiling changes later on anyway.)
1 parent e160ba5 commit 040686f

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

cargo-fix/src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ fn log_message(msg: &Message, stream: &mut StandardStream) -> Result<(), Error>
102102
stream,
103103
)?;
104104
}
105+
ReplaceFailed { ref file, ref message } => {
106+
stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)))?;
107+
write!(stream, "warning")?;
108+
stream.reset()?;
109+
stream.set_color(ColorSpec::new().set_bold(true))?;
110+
write!(stream, ": error applying suggestions to `{}`\n", file)?;
111+
stream.reset()?;
112+
write!(stream, "The full error message was:\n\n> {}\n\n", message)?;
113+
stream.write(PLEASE_REPORT_THIS_BUG.as_bytes())?;
114+
}
105115
FixFailed { ref files, ref krate } => {
106116
stream.set_color(ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)))?;
107117
write!(stream, "warning")?;

cargo-fix/src/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static DIAGNOSICS_SERVER_VAR: &str = "__CARGO_FIX_DIAGNOSTICS_SERVER";
1717
pub enum Message {
1818
Fixing { file: String, fixes: usize },
1919
FixFailed { files: Vec<String>, krate: Option<String> },
20+
ReplaceFailed { file: String, message: String },
2021
}
2122

2223
impl Message {

cargo-fix/src/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,19 @@ fn rustfix_crate(rustc: &Path, filename: &str) -> Result<FixedCrate, Error> {
229229

230230
messages.push(Message::fixing(&file, num_suggestions));
231231

232-
let new_code = rustfix::apply_suggestions(&code, &suggestions)?;
233-
File::create(&file)
234-
.and_then(|mut f| f.write_all(new_code.as_bytes()))
235-
.with_context(|_| format!("failed to write file `{}`", file))?;
236-
original_files.insert(file, code);
232+
match rustfix::apply_suggestions(&code, &suggestions) {
233+
Err(e) => {
234+
diagnostics::Message::ReplaceFailed { file: file, message: e.to_string() }.post()?;
235+
// TODO: Add flag to decide if we want to continue or bail out
236+
continue;
237+
}
238+
Ok(new_code) => {
239+
File::create(&file)
240+
.and_then(|mut f| f.write_all(new_code.as_bytes()))
241+
.with_context(|_| format!("failed to write file `{}`", file))?;
242+
original_files.insert(file, code);
243+
}
244+
}
237245
}
238246

239247
Ok(FixedCrate {

cargo-fix/tests/all/broken_lints.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Ensure we give good error message when rustfix failes to apply changes
2+
//!
3+
//! TODO: Add rustc shim that outputs wrong suggestions instead of depending on
4+
//! actual rustc bugs!
5+
6+
use super::project;
7+
8+
#[test]
9+
fn tell_user_about_broken_lints() {
10+
let p = project()
11+
.file(
12+
"src/lib.rs",
13+
r#"
14+
pub fn foo() {
15+
let mut i = 42;
16+
}
17+
"#,
18+
)
19+
.build();
20+
21+
p.expect_cmd("cargo-fix fix")
22+
.stderr_contains(r"warning: error applying suggestions to `src/lib.rs`")
23+
.stderr_contains("The full error message was:")
24+
.stderr_contains("> Could not replace range 56...60 in file -- maybe parts of it were already replaced?")
25+
.stderr_contains("\
26+
This likely indicates a bug in either rustc or rustfix itself,\n\
27+
and we would appreciate a bug report! You're likely to see \n\
28+
a number of compiler warnings after this message which rustfix\n\
29+
attempted to fix but failed. If you could open an issue at\n\
30+
https://github.com/rust-lang-nursery/rustfix/issues\n\
31+
quoting the full output of this command we'd be very appreciative!\n\n\
32+
")
33+
.status(0)
34+
.run();
35+
}

cargo-fix/tests/all/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ fn diff(expected: &str, actual: &str) {
316316
}
317317

318318
mod broken_build;
319+
mod broken_lints;
319320
mod dependencies;
320321
mod smoke;
321322
mod subtargets;

0 commit comments

Comments
 (0)