Skip to content
Latchkey

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

Terminal
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-run

Flags

FlagWhat it does
--fixApply machine-applicable suggestions in place
--allow-dirtyPermit running with uncommitted changes
--allow-stagedPermit running with staged changes
--dry-runShow fixes without writing them
--broken-codeApply 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.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →