Sub topic 8
Applying suggested fixes automatically
This chapter assumes you can run cargo clippy and read its warnings, and that your project is in Git. Many Clippy warnings come with a ready-made code change, and Cargo can apply those changes for you. You will learn when that is safe, how to run it, and how to review the result.
How suggestions carry an applicability
Each Clippy suggestion is tagged with an applicability level. Two levels matter in practice:
- MachineApplicable: the tool is confident the change keeps the program's meaning.
--fixapplies these. - MaybeIncorrect: the change is a good guess but may alter behavior or need a human decision.
--fixskips these, and you apply them by hand.
A warning that shows a help: line with replacement code may be either kind. Whether --fix applies it depends on the lint's applicability, not on how the suggestion looks in the terminal.
Running cargo clippy --fix
Start with a small file that triggers two lints, needless_return and len_zero:
fn is_blank(s: &str) -> bool {
return s.len() == 0;
}
fn main() {
println!("{}", is_blank(""));
}Run Clippy with the fix flag:
cargo clippy --fixCargo refuses to touch files when the working tree has uncommitted changes, because it cannot tell your edits from its own. Commit first, or pass a flag to override the check:
cargo clippy --fix --allow-dirty
cargo clippy --fix --allow-staged--allow-dirty permits unstaged changes. --allow-staged permits changes that are staged but not committed. Use them only when you can still recover your work, for example when the changes are saved in a stash or another commit.
After the run, is_blank becomes:
fn is_blank(s: &str) -> bool {
s.is_empty()
}
fn main() {
println!("{}", is_blank(""));
}Both lints were machine-applicable, so both were fixed in one pass.
Reviewing the result
An automatic fix is a code change like any other, so check it before you keep it.
- Run
git diffand read every hunk. Look for changes that touch more than you expected. - Run
cargo buildandcargo test. A fix can compile and still change behavior in an edge case. - Run
cargo fmt. Rewritten code is not always formatted to your style. - Commit the fixes separately from feature work, so a reviewer can skim them.
Committing the fixes on their own also makes them easy to revert. If one fix is wrong, git checkout -p lets you discard single hunks.
Controlling what gets fixed
cargo clippy --fix only edits code that Cargo actually compiled. Code behind a feature flag or inside tests is skipped unless you include it:
cargo clippy --fix --all-targets --all-features --allow-dirty--fix also applies rustc's own machine-applicable suggestions, such as removing an unused import. Your diff may therefore contain changes that are not Clippy lints.
To fix one lint only, enable that lint and silence the rest through the usual flags:
cargo clippy --fix --allow-dirty -- -A clippy::all -W clippy::len_zeroHere -A clippy::all allows every default lint, and -W clippy::len_zero warns on the one you want. Only the lints that are still active can produce fixes.
Common problems
| Symptom | Cause | Remedy |
|---|---|---|
| Error about uncommitted changes | The working tree is dirty | Commit, or add `--allow-dirty` or `--allow-staged` |
| Warning remains after `--fix` | Its suggestion is `MaybeIncorrect` or has none | Apply the `help:` change by hand |
| Warning in a `#[cfg(test)]` module remains | Tests were not compiled | Add `--all-targets` |
| Warning behind a feature remains | The feature was off | Add `--all-features` |
| New warnings appear after a fix | One fix exposed another pattern | Run `cargo clippy --fix` again |
Exercise
You run cargo clippy --fix and get an error that the working directory is dirty. After you commit, the command succeeds, but one warning about a loop is still printed. Give two reasons the warning might remain, and say how you would handle each.
Show answer
The first reason is that the lint's suggestion is MaybeIncorrect or has no suggestion, so --fix does not apply it. Read the help: text and edit the code by hand. The second reason is that the code is in a target or feature that was not compiled, such as tests or an optional feature. Re-run with --all-targets --all-features. After either change, review git diff and run the tests.