cargo clippy --fix: Autofix Rust Lints
cargo clippy --fix rewrites your code in place for every suggestion Clippy marks as machine-applicable.
Many Clippy lints carry an automatic fix. The --fix flag applies them, which is handy locally and in a "lint-fix" CI job that opens a PR.
What it does
cargo clippy --fix runs the lints and applies suggestions flagged MachineApplicable, editing files directly. By default it refuses to run on a dirty or staged working tree so it does not mix its edits with yours.
Common usage
cargo clippy --fix --all-targets --all-features
# allow running with uncommitted or staged changes
cargo clippy --fix --allow-dirty --allow-staged
# check what would change without editing
cargo clippy --fix --dry-runFlags
| Flag | What it does |
|---|---|
| --fix | Apply machine-applicable suggestions in place |
| --allow-dirty | Permit running with uncommitted changes |
| --allow-staged | Permit running with staged changes |
| --dry-run | Show fixes without writing them |
| --broken-code | Apply fixes even if the result may not compile |
In CI
A fix job typically runs clippy --fix --allow-dirty on a fresh checkout, then commits the diff or fails if the tree changed. Not all lints are auto-fixable, so always run a plain clippy -- -D warnings afterward to confirm nothing remains.
Common errors in CI
"error: the working directory of this project has uncommitted changes" means the tree is dirty; add --allow-dirty (and --allow-staged if files are staged). "warning: failed to automatically apply fixes" means suggestions conflicted; rerun, or fix those lints by hand.