Skip to content
Latchkey

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

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

Flags

FlagWhat it does
--all-targetsLint lib, bins, tests, examples, and benches
--all-featuresEnable all Cargo features before linting
--workspaceLint every crate in the workspace
-- -D warningsPass lint flags; deny all warnings
--fixApply machine-applicable suggestions
--message-format=jsonEmit 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.

Related guides

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