Skip to content
Latchkey

cargo clippy "-D warnings" fails the build in CI

CI commonly runs cargo clippy -- -D warnings, which turns every clippy lint into a hard error. Any lint then makes the crate fail to compile, and cargo exits 101.

What this error means

clippy prints "error: ..." for what would be warnings, each with a "#[deny(clippy::X)] on by default" note, and ends with "error: could not compile crate due to N previous errors".

clippy
error: this expression creates a reference which is immediately dereferenced by the compiler
  --> src/main.rs:7:14
   = note: `-D clippy::needless-borrow` implied by `-D warnings`

error: could not compile `app` (bin "app") due to 1 previous error

Common causes

Warnings are denied, so lints are errors

-D warnings (or RUSTFLAGS=-Dwarnings) promotes every clippy and rustc warning to an error, so even minor lints fail the build.

New lints from a newer clippy/toolchain

A toolchain bump ships new or stricter clippy lints that now fire on existing code under the deny gate.

How to fix it

Fix the lints (autofix where possible)

  1. Run cargo clippy --fix to apply machine-applicable suggestions.
  2. Address the remaining lints by hand using each suggestion.
  3. Re-run with -D warnings to confirm a clean compile.
Terminal
cargo clippy --fix --allow-dirty
cargo clippy -- -D warnings

Allow a specific lint deliberately

If a lint is a false positive, allow just that lint with an attribute rather than dropping -D warnings.

src/main.rs
#[allow(clippy::needless_borrow)]
fn f() { /* ... */ }

How to prevent it

  • Run cargo clippy -- -D warnings locally before pushing.
  • Pin the Rust toolchain so new lints arrive on purpose.
  • Scope #[allow(...)] narrowly instead of disabling the gate.

Frequently asked questions

What causes "clippy "could not compile due to N previous errors""?
-D warnings (or RUSTFLAGS=-Dwarnings) promotes every clippy and rustc warning to an error, so even minor lints fail the build.
How do I fix clippy "could not compile due to N previous errors"?
Fix the lints (autofix where possible)

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card