Sub topic 12
Using compiler suggestions and cargo fix safely
This chapter assumes you can read the parts of a diagnostic (code, message, span, labels, notes, help) and run cargo check. It covers which suggestions you can trust and how to apply them with cargo fix.
Where suggestions come from
Many diagnostics end with a help: line. Some also show a corrected snippet with + and - marks. Each suggestion carries an applicability level that tells tools how far to trust it:
| Level | Meaning | Applied by `cargo fix`? |
|---|---|---|
| MachineApplicable | Safe to apply without review | Yes |
| MaybeIncorrect | Might compile but not do what you meant | No |
| HasPlaceholders | Contains a part you must fill in | No |
| Unspecified | No promise either way | No |
You don't see these labels in the terminal. They matter because cargo fix reads them. A human still has to read every other help: line.
A suggestion that is usually right
The semicolon error from earlier chapters shows a typical case.
fn double(x: i32) -> i32 {
x * 2;
}
fn main() {
println!("{}", double(4));
}rustc reports E0308 and suggests removing the semicolon. Here the advice matches the intent, because the function clearly meant to return x * 2. Check that this is true each time. A suggestion fixes the error the compiler sees, not necessarily the bug you have.
A suggestion that compiles but may be wrong
For a use-after-move error (E0382), rustc may suggest .clone(). That removes the error, but it copies the data. If the copy was never needed, borrowing with &name is the better fix. Ask what the code should mean before accepting the compiler's quickest route.
Running cargo fix
cargo fix runs the checker and edits your files for you. It applies only machine-applicable suggestions. Create a file with two warnings:
use std::collections::HashMap;
fn main() {
let mut count = 3;
println!("{count}");
}Run:
git status
cargo fix
git diffThe first warning is an unused import. The second is unused_mut, because count is never changed. cargo fix removes the import and the mut. Then git diff shows exactly what changed.
cargo fix refuses to run if you have uncommitted changes. The --allow-dirty and --allow-staged flags override this check. Prefer to commit first, so the diff contains only the tool's edits.
Common pitfalls
cargo fixhandles warnings and a few errors that have machine-applicable fixes. It doesn't repair borrow errors or lifetime errors. You still fix those yourself.- It checks only the code compiled for your current target and features. Code behind a
#[cfg]that is off stays unchanged. Use--all-targetsto include tests and examples. cargo fix --editionmigrates code to a new Rust edition. Run it separately from other changes so the diff stays easy to review.cargo clippy --fixapplies Clippy's machine-applicable suggestions in the same way.
Summary of habits
- Read the full diagnostic, not just the
help:line. - Decide whether the suggestion matches what the code should do.
- Commit before
cargo fix, then review the diff. - Run
cargo checkand your tests afterward.
Exercise
You run cargo fix on a project and it reports that the working directory has uncommitted changes and stops. What is the safest way to proceed, and why is --allow-dirty a poor first choice?
Show answer
Commit or stash your current work, then run cargo fix again. With a clean tree, git diff shows only the tool's edits, so you can review them and revert with git restore if needed. --allow-dirty mixes the tool's edits with your own uncommitted work, so you can't separate them afterward.
Exercise
rustc reports E0382 on a moved String and suggests .clone(). Name one reason you might not accept that suggestion.
Show answer
The clone copies the heap data, which costs time and memory. If the later use only needs to read the value, borrowing with & avoids the copy, and it states the intent more clearly.