Skip to content

Commit f986d10

Browse files
committed
fix(ops): improve warning for --broken-code when fixes were applied
1 parent b54fe55 commit f986d10

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

src/cargo/ops/fix/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ pub fn fix_exec_rustc(gctx: &GlobalContext, lock_addr: &str) -> CargoResult<()>
814814
krate,
815815
&fixes.last_output.stderr,
816816
fixes.last_output.status,
817+
allow_broken_code,
817818
)?;
818819
// Display the diagnostics that appeared at the start, before the
819820
// fixes failed. This can help with diagnosing which suggestions
@@ -1171,6 +1172,7 @@ fn log_failed_fix(
11711172
krate: Option<String>,
11721173
stderr: &[u8],
11731174
status: ExitStatus,
1175+
allow_broken_code: bool,
11741176
) -> CargoResult<()> {
11751177
let stderr = str::from_utf8(stderr).context("failed to parse rustc stderr as utf-8")?;
11761178

@@ -1205,6 +1207,7 @@ fn log_failed_fix(
12051207
krate,
12061208
errors,
12071209
abnormal_exit,
1210+
allow_broken_code,
12081211
}
12091212
.post(gctx)?;
12101213

src/cargo/util/diagnostic_server.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum Message {
4343
krate: Option<String>,
4444
errors: Vec<String>,
4545
abnormal_exit: Option<String>,
46+
allow_broken_code: bool,
4647
},
4748
ReplaceFailed {
4849
file: String,
@@ -146,6 +147,7 @@ impl<'a> DiagnosticPrinter<'a> {
146147
krate,
147148
errors,
148149
abnormal_exit,
150+
allow_broken_code,
149151
} => {
150152
let to_crate = if let Some(ref krate) = *krate {
151153
format!(" to crate `{krate}`",)
@@ -160,28 +162,36 @@ impl<'a> DiagnosticPrinter<'a> {
160162
None
161163
};
162164

163-
let report = &[
164-
Level::ERROR
165-
.secondary_title(format!("errors present after applying fixes{to_crate}"))
166-
.elements(files.iter().map(|f| Origin::path(f)))
167-
.elements(
168-
cause_message
169-
.into_iter()
170-
.map(|err| Level::ERROR.with_name("cause").message(err)),
171-
)
172-
.elements(abnormal_exit.iter().map(|exit| {
173-
Level::ERROR
174-
.with_name("cause")
175-
.message(format!("rustc exited abnormally: {exit}"))
176-
})),
177-
gen_please_report_this_bug_group(issue_link),
178-
gen_suggest_broken_code_group(),
179-
Group::with_title(
180-
Level::NOTE.secondary_title("original diagnostics will follow:"),
181-
),
182-
];
165+
let report = &[Level::ERROR
166+
.secondary_title(format!("errors present after applying fixes{to_crate}"))
167+
.elements(files.iter().map(|f| Origin::path(f)))
168+
.elements(
169+
cause_message
170+
.into_iter()
171+
.map(|err| Level::ERROR.with_name("cause").message(err)),
172+
)
173+
.elements(abnormal_exit.iter().map(|exit| {
174+
Level::ERROR
175+
.with_name("cause")
176+
.message(format!("rustc exited abnormally: {exit}"))
177+
}))];
178+
179+
let mut report = report.to_vec();
180+
181+
if *allow_broken_code {
182+
report.push(Group::with_title(Level::WARNING.secondary_title(
183+
"fixes were applied but the code still does not compile; partially-fixed code was saved due to `--broken-code`"
184+
)));
185+
} else {
186+
report.push(gen_please_report_this_bug_group(issue_link));
187+
report.push(gen_suggest_broken_code_group());
188+
}
183189

184-
self.gctx.shell().print_report(report, false)?;
190+
report.push(Group::with_title(
191+
Level::NOTE.secondary_title("original diagnostics will follow:"),
192+
));
193+
194+
self.gctx.shell().print_report(&report, false)?;
185195
Ok(())
186196
}
187197
Message::EditionAlreadyEnabled { message, edition } => {

tests/testsuite/fix.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,8 +1435,7 @@ fn fix_to_broken_code() {
14351435
= cause: thread 'main' ([..]) panicked at src/main.rs:23:29:
14361436
explicit panic
14371437
[NOTE] run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1438-
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
1439-
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
1438+
[WARNING] fixes were applied but the code still does not compile; partially-fixed code was saved due to `--broken-code`
14401439
[NOTE] original diagnostics will follow:
14411440
...
14421441

tests/testsuite/fix_n_times.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ fn broken_code_one_suggestion() {
571571
[ERROR] errors present after applying fixes to crate `foo`
572572
--> src/lib.rs
573573
= cause: rustc fix shim error count=2
574-
[HELP] to report this as a bug, open an issue at https://github.com/rust-lang/rust/issues, quoting the full output of this command
575-
[HELP] to possibly apply more fixes, pass in the `--broken-code` flag
574+
[WARNING] fixes were applied but the code still does not compile; partially-fixed code was saved due to `--broken-code`
576575
[NOTE] original diagnostics will follow:
577576
rustc fix shim comment 1
578577
rustc fix shim error count=2

0 commit comments

Comments
 (0)