Description
Consider the following code:
fn main() {
assert!(true);
}
fn dead() -> u32 {
42
}
I would expect cargo rustc -- -Dwarnings -Wdead-code
to merely warn about the dead code (because the -W
comes after the -D
and thus should "reset" the dead-code lint's level back to "warn"), but it actually ignores the -W
option and treats the dead code as an error:
Compiling linting v0.1.0 (/Users/jwodder/work/dev/tmp/linting)
error: function `dead` is never used
--> src/main.rs:5:4
|
5 | fn dead() -> u32 {
| ^^^^
|
= note: `-D dead-code` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(dead_code)]`
error: could not compile `linting` (bin "linting") due to previous error
Contrast with cargo rustc -- -Dunused -Wdead-code
, which does treat the final lint level of dead-code as "warn" rather than "deny":
Compiling linting v0.1.0 (/Users/jwodder/work/dev/tmp/linting)
warning: function `dead` is never used
--> src/main.rs:5:4
|
5 | fn dead() -> u32 {
| ^^^^
|
= note: requested on the command line with `-W dead-code`
warning: `linting` (bin "linting") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
-Dwarnings
even has the same effect on non-rustc warnings, such as from clippy. For example, running cargo clippy -- -Dwarnings -W clippy::style
on the code above causes the assert!(true)
to be treated as an error rather than a warning; contrast with cargo clippy -- -D clippy::all -W clippy::style
, which first denies everything in the style group (among others) and then sets the style group's lint level back to "warn", with an end result of the assert!(true)
only producing a warning.
In addition, as far as I understand it, the [lints]
table added to Cargo.toml
in Rust 1.74 is implemented by converting the lint fields to command-line options for rustc, rustdoc, and clippy, and so anyone trying to deny all warn-by-default lints with some exceptions is up a creek.
Meta
rustc --version --verbose
:
rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-apple-darwin
release: 1.74.0
LLVM version: 17.0.4
rustc +nightly --version --verbose
:
rustc 1.76.0-nightly (3a85a5cfe 2023-11-20)
binary: rustc
commit-hash: 3a85a5cfe7884f94e3cb29a606913d7989ad9b48
commit-date: 2023-11-20
host: x86_64-apple-darwin
release: 1.76.0-nightly
LLVM version: 17.0.5