Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo fix should suggest --broken-code #15063

Open
Wilfred opened this issue Jan 14, 2025 · 1 comment
Open

cargo fix should suggest --broken-code #15063

Wilfred opened this issue Jan 14, 2025 · 1 comment
Labels
C-bug Category: bug Command-fix S-triage Status: This issue is waiting on initial triage.

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Jan 14, 2025

Problem

cargo fix makes it hard to discover the --broken-code option. Running cargo fix when there are compile errors gives no hints about this option, and it's not terribly obvious from cargo help fix either. I'm sure there are users missing out on cargo fix as a result -- I certainly was :)

Steps

Given a library crate with src/lib.rs containing:

fn takes_ref(_x: &usize) {}
    
pub fn call_it() {
    let i = 1usize;
    takes_ref(i); // oops
}

I get the following output:

$ cargo fix --allow-dirty       
    Checking mylib v0.1.0 (/tmp/mylib)
error[E0308]: mismatched types
 --> src/lib.rs:5:15
  |
5 |     takes_ref(i); // oops
  |     --------- ^ expected `&usize`, found `usize`
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/lib.rs:1:4
  |
1 | fn takes_ref(_x: &usize) {}
  |    ^^^^^^^^^ ----------
help: consider borrowing here
  |
5 |     takes_ref(&i); // oops
  |               +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `mylib` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `mylib` (lib) due to 1 previous error

This is really confusing: rustc knows how to fix my code, it says so, and yet cargo fix isn't doing anything.

Possible Solution(s)

Ideally, there'd be a hint:

note: `cargo fix` requires `--broken-code` to apply suggestions when there are compiler errors.

(Whilst this is potentially a destructive option, cargo fix already suggests other destructive options like --allow-dirty.)

The output of cargo help fix is:

NAME
       cargo-fix — Automatically fix lint warnings reported by rustc

SYNOPSIS
       cargo fix [options]

DESCRIPTION
       This Cargo subcommand will automatically take rustc’s suggestions from diagnostics like warnings and apply them to your source code. This is intended to help automate tasks that
       rustc itself already knows how to tell you to fix!

This wording isn't ideal. "Automatically fix lint warnings" suggests that cargo fix can only do lints. On the other hand, "automatically take rustc’s suggestions diagnostics like warnings" suggests that cargo fix will do more than just warnings.

The description doesn't actually mention --broken-code, nor is this flag shown in the EXAMPLES section of the help.

Notes

I apologise if this seems super nitpicky! For a long time I didn't realise that cargo fix --broken-code could do exactly what I wanted (just apply all rustc suggestions), so I figured I'd report an issue.

Version

$ cargo version --verbose  
cargo 1.84.0 (66221abde 2024-11-19)
@Wilfred Wilfred added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Jan 14, 2025
@epage
Copy link
Contributor

epage commented Jan 14, 2025

Historically, we've not been too confident in the suggestions when errors are present. If we had --interactive support (#11125), then we could offer the user every suggestion, whether there is an error, or if the suggestion is marked as a maybe applicable.

We also have some UX issues around --broken-code, see #10955.

Granted, sometimes the only way to get something to be improved is to make it front and center.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-fix S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

No branches or pull requests

2 participants