Skip to content

Commit fccbe15

Browse files
committed
fix(ops): add warning when no fixes suggested with --broken-code
1 parent f986d10 commit fccbe15

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

crates/cargo-util-terminal/src/shell.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ impl Shell {
233233
self.print_report(report, false)
234234
}
235235

236+
/// Prints a cyan 'help' message.
237+
pub fn help<T: fmt::Display>(&mut self, message: T) -> CargoResult<()> {
238+
let report = &[annotate_snippets::Group::with_title(
239+
annotate_snippets::Level::HELP.secondary_title(message.to_string()),
240+
)];
241+
self.print_report(report, false)
242+
}
243+
236244
/// Updates the verbosity of the shell.
237245
pub fn set_verbosity(&mut self, verbosity: Verbosity) {
238246
self.verbosity = verbosity;

src/cargo/ops/fix/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,26 @@ pub fn fix_exec_rustc(gctx: &GlobalContext, lock_addr: &str) -> CargoResult<()>
791791

792792
// If there were any fixes, let the user know that there was a failure
793793
// attempting to apply them, and to ask for a bug report.
794-
//
795-
// FIXME: The error message here is not correct with --broken-code.
796-
// https://github.com/rust-lang/cargo/issues/10955
797794
if fixes.files.is_empty() {
795+
if !fixes.last_output.status.success() {
796+
if allow_broken_code {
797+
gctx.shell()
798+
.warn("no fixes were suggested because the code is already broken")?;
799+
gctx.shell().note(
800+
"cargo fix requires code that compiles successfully to apply automatic fixes",
801+
)?;
802+
gctx.shell()
803+
.note("the broken code was saved due to `--broken-code`")?;
804+
} else {
805+
gctx.shell()
806+
.warn("no fixes were suggested because the code is already broken")?;
807+
gctx.shell().note(
808+
"cargo fix requires code that compiles successfully to apply automatic fixes",
809+
)?;
810+
gctx.shell()
811+
.help("use `--broken-code` to save partial progress")?;
812+
}
813+
}
798814
// No fixes were available. Display whatever errors happened.
799815
emit_output(&fixes.last_output)?;
800816
exit_with(fixes.last_output.status);

tests/testsuite/fix.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,9 @@ fn fix_in_rust_src() {
23802380
.with_status(101)
23812381
.with_stderr_data(str![[r#"
23822382
[CHECKING] foo v0.0.0 ([ROOT]/foo)
2383+
[WARNING] no fixes were suggested because the code is already broken
2384+
[NOTE] cargo fix requires code that compiles successfully to apply automatic fixes
2385+
[NOTE] the broken code was saved due to `--broken-code`
23832386
error[E0308]: mismatched types
23842387
--> lib.rs:5:9
23852388
|

tests/testsuite/fix_n_times.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ fn starts_with_error() {
532532
},
533533
str![[r#"
534534
[CHECKING] foo v0.0.1 ([ROOT]/foo)
535+
[WARNING] no fixes were suggested because the code is already broken
536+
[NOTE] cargo fix requires code that compiles successfully to apply automatic fixes
537+
[HELP] use `--broken-code` to save partial progress
535538
rustc fix shim error count=1
536539
[ERROR] could not compile `foo` (lib) due to 1 previous error
537540
@@ -550,6 +553,9 @@ fn broken_code_no_suggestions() {
550553
},
551554
str![[r#"
552555
[CHECKING] foo v0.0.1 ([ROOT]/foo)
556+
[WARNING] no fixes were suggested because the code is already broken
557+
[NOTE] cargo fix requires code that compiles successfully to apply automatic fixes
558+
[NOTE] the broken code was saved due to `--broken-code`
553559
rustc fix shim error count=1
554560
[ERROR] could not compile `foo` (lib) due to 1 previous error
555561

0 commit comments

Comments
 (0)