Skip to content

-Dwarnings cannot be overriden by later -W options, for both rustc and non-rustc lints #118140

Open
@jwodder

Description

@jwodder

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-CLIArea: Command-line interface (CLI) to the compilerA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions