cargo clippy: Lint Rust Code in CI
cargo clippy compiles your crate with the Clippy lint pass enabled and reports style and correctness lints beyond what rustc gives you.
Clippy is the standard Rust linter, distributed as a rustup component. In CI you run it across all targets and features and gate on warnings.
What it does
cargo clippy builds the crate the way cargo check does but with Clippy lints turned on. Flags before -- go to cargo; flags after -- go to the lint driver. It exits non-zero only on errors, so warnings must be promoted to fail a build.
Common usage
cargo clippy --all-targets --all-features
# fail the build on any warning
cargo clippy --all-targets --all-features -- -D warnings
# only the library, no tests or examples
cargo clippy --libFlags
| Flag | What it does |
|---|---|
| --all-targets | Lint lib, bins, tests, examples, and benches |
| --all-features | Enable all Cargo features before linting |
| --workspace | Lint every crate in the workspace |
| -- -D warnings | Pass lint flags; deny all warnings |
| --fix | Apply machine-applicable suggestions |
| --message-format=json | Emit JSON diagnostics for tooling |
In CI
Add the clippy component with rustup component add clippy. Without -- -D warnings, clippy prints warnings but exits 0, so the job passes despite lint issues; the deny flag is what makes CI fail on them.
Common errors in CI
"error: the 'cargo-clippy' binary ... can only be used with the rustup-managed toolchain" or "error: no such command: clippy" means the component is not installed; run rustup component add clippy. "could not compile <crate> ... due to N previous errors" after -D warnings means lints were promoted to errors as intended.