rust-lang / rust-lang/rust

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

Open
#118,140 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-CLI A-docs A-lints C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the cargo rustc and cargo clippy command lines in the report, then trace how -Dwarnings, later -W options, and the [lints] table in Cargo.toml are converted and applied. Done means a later -W resets the selected lint group to warnings consistently for rustc and clippy without breaking the reported -Dunused behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.