After a Rust toolchain bump, CI running `cargo clippy -- -D warnings` fails on code nobody changed; why, and how would you set the warning gate up instead?
answer
- new toolchain, new lints
- -D warnings promotes all of them
- stale expectations and renamed lints
- flags change what is cached
- build.warnings since Cargo 1.97
basics
~20 sRust releases ship new rustc and Clippy lints and rename or fix old ones, and -D warnings turns every resulting warning into an error. Pin the toolchain, bump it in its own pull request, and deny warnings in CI with CARGO_BUILD_WARNINGS=deny.
solid answer
~50 s`-D warnings` promises warning-free code *under the current toolchain*. A new release adds lints to the default groups, uplifts Clippy lints into rustc, renames or removes others (reported by `renamed_and_removed_lints`), and fixes false positives so an `#[expect]` becomes unfulfilled. Each of those is a warning, and each is promoted to an error on code nobody touched. Contain it: pin the toolchain so a bump is a deliberate pull request that carries its lint fixes, and keep levels in the `[lints]` table. Then change how warnings are denied. Since Cargo 1.97 the recommended gate is `build.warnings = "deny"`, set in CI as `CARGO_BUILD_WARNINGS=deny`: it applies to local packages only and fails on cached warnings without recompiling. `cargo clippy -- -D warnings` changes Clippy's arguments, so local crates are re-checked whenever it alternates with a plain run, and `RUSTFLAGS=-Dwarnings` changes every crate's flags, so dependencies are rebuilt too.
code
bash · 6 lines#!/usr/bin/env bash
set -euo pipefail
export CARGO_BUILD_WARNINGS=deny
cargo fmt --check
cargo clippy --all-targets --locked
cargo test --lockedgo deeper
Recall that -D warnings turns every warning into an error, and that new Rust releases bring new lints.
Explain the ways warnings get denied (build.warnings since Cargo 1.97, -- -D warnings, RUSTFLAGS) and what each one covers.
Diagnose the red build as a toolchain change, not a code change, and set up a pinned toolchain, a bump pull request and CARGO_BUILD_WARNINGS=deny in CI, citing the cache cost of RUSTFLAGS.
Decide how fast the team follows stable Rust: a pinned toolchain with scheduled bumps trades new lints and fixes for predictability, and the lead owns that cadence.
## Why unchanged code starts failing A warning gate such as `cargo clippy -- -D warnings` does not test the code; it tests the code **together with the toolchain's lint set**. Change the toolchain and the result can change even if no source file did. A new Rust release can: - add a new lint to a warn-by-default Clippy group (Clippy lands new lints on nightly first, and they reach stable releases later); - **uplift** a Clippy lint into rustc, so it fires in `cargo build` as well; - rename or remove a lint, which makes an old `#[allow(clippy::box_vec)]` report `renamed_and_removed_lints` (warn-by-default); - fix a false positive, so an `#[expect(...)]` that excused it becomes unfulfilled and reports `unfulfilled_lint_expectations`; - add rustc warnings, deprecations or future-incompatibility warnings that apply to existing code. Every one of those is a warning, and `-D warnings` turns every warning into an error. The job goes red on the first run with the new toolchain, and it can go red on a pull request that only touched documentation if CI tracks the latest stable. ## Contain the toolchain side 1. **Pin the toolchain** the project builds with (a `rust-toolchain.toml` channel, covered with release channels and MSRV), so every developer and CI job uses the same Clippy. 2. **Bump it in its own pull request.** That pull request runs Clippy, and its lint fixes or new `allow` entries land with it. Nothing else is blocked. 3. **Optionally run beta or nightly Clippy as a non-blocking job**, which Clippy's CI chapter suggests, to see next release's lints early. 4. **Set `rust-version` in the manifest** so Clippy, which takes its `msrv` from it by default, does not suggest APIs newer than the version you support. ## Three ways to deny warnings, measured The same crate (one dependency, one `clippy::len_zero` warning) was checked with cargo 1.99.1 after a plain `cargo clippy` had filled the cache: | mechanism | what it covers | what it cost on a warm cache | |---|---|---| | `CARGO_BUILD_WARNINGS=deny` (or `[build] warnings = "deny"`) | lint warnings of local packages, in build, check, clippy, test and doc runs | nothing re-checked; the cached warning was replayed and the run failed | | `cargo clippy -- -D warnings` | Clippy runs only | the local crate was re-checked, because Clippy's arguments changed | | `RUSTFLAGS=-Dwarnings` | every rustc invocation, including builds and tests | the dependency (`itoa`) and the local crate were both re-checked | `build.warnings` was stabilised in **Cargo 1.97**, and the Clippy book now recommends it over `-D warnings`, noting that the flag invalidates build caches. Its failure message is `error: warnings are denied by `build.warnings` configuration`. The documented scope matters too: it affects only warnings that are lints in **local packages**; non-lint warnings and dependency warnings are left as they were. ## Reading a red build after a bump When the job fails, the message tells you which kind of change you are looking at: - a lint name you have never seen, with `on by default`, is a **new lint**: fix the code or allow it in `[lints]` in the bump pull request; - `renamed_and_removed_lints` points at an attribute or table entry that uses an **old name**: rename or delete it; - `unfulfilled_lint_expectations` points at an `#[expect]` whose lint **no longer fires**, often because a false positive was fixed: delete the attribute; - a rustc deprecation or future-incompatibility warning is a **language or library change**: read the release notes before silencing it. ## Where to set it - Set `CARGO_BUILD_WARNINGS=deny` in the CI environment, not `warnings = "deny"` in a committed `.cargo/config.toml`, so a developer's half-finished local build is not blocked by an unused variable. - Keep lint **levels** in `[lints]`/`[workspace.lints]`; keep the **gate** in CI. The two are separate decisions. - Cover tests, examples and benches with `--all-targets`, and keep `--locked` so CI uses the committed `Cargo.lock`. ## A workable gate 1. `cargo fmt --check` 2. `cargo clippy --all-targets --locked` with `CARGO_BUILD_WARNINGS=deny` 3. `cargo test --locked`, under the same setting, so rustc warnings in test code fail too 4. A separate, allowed-to-fail job on the next toolchain, read before each bump This keeps the strict gate the team wants while removing both failure modes: surprise lints arrive in a planned pull request, and denying warnings no longer forces a rebuild.
- Why not put `warnings = "deny"` under `[build]` in the repository's `.cargo/config.toml`?That file applies to everyone who builds in the repository, so a developer's half-finished change with an unused variable would fail `cargo build` and `cargo test` locally. Setting `CARGO_BUILD_WARNINGS=deny` only in the CI environment keeps the gate strict where it matters and leaves local work at the default `warn`.
- With `build.warnings = "deny"`, does a warning inside a crates.io dependency fail the build?No. The setting adjusts only lint warnings of local packages, the workspace members and path dependencies Cargo builds as yours. Registry dependencies are built with capped lints, and the Cargo documentation states that warnings from dependencies are left as they are.
- A bump turns an `#[allow(clippy::box_vec)]` into a warning; what is it, and what do you do?The lint was renamed or removed, so `cargo clippy` reports `renamed_and_removed_lints` (for example ``lint `clippy::box_vec` has been renamed to `clippy::box_collection` ``), which is warn-by-default and therefore fails a deny-warnings gate. Change the attribute to the new name the message suggests, or delete it if the lint is gone, in the toolchain-bump pull request.
- Code that passed -D warnings once will keep passing on every future toolchain
- RUSTFLAGS=-Dwarnings only affects local crates, so it costs no extra build time
- build.warnings = "deny" also fails the build on warnings in crates.io dependencies
- The fix for new lints is to stop running Clippy in CI
- Tracking the latest stable toolchain in CI without pinning is the safest default
Assumes Cargo 1.99; build.warnings (CARGO_BUILD_WARNINGS) is stable since Cargo 1.97 and is now the Clippy book's recommended replacement for -D warnings.