Skip to content

cargo fix should suggest --broken-code #15063

Open
@Wilfred

Description

@Wilfred

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugCommand-fixS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions