Skip to content

Diagnostics for -D flags make it slightly harder to allow vs. #![deny(...)] #114030

Closed

Description

When using -D command-line flags to deny some lints, the compiler emits diagnostics that are slightly harder to allow, compared to a denied lint via #![deny(...)].

Compare the following cases:

echo "struct S;"                      | rustc --crate-type=lib             -
echo "#![deny(dead_code)]\nstruct S;" | rustc --crate-type=lib             -
echo "struct S;"                      | rustc --crate-type=lib -Ddead_code -
echo "struct S;"                      | rustc --crate-type=lib -Dwarnings  -

The first two provide, one way or another, the name of the lint that can be easily copy-pasted from the terminal to allow(...) it. In particular, they provide the name of the lint with underscores:

warning: struct `S` is never constructed
 --> <anon>:1:8
  |
1 | struct S;
  |        ^
  |
  = note: `#[warn(dead_code)]` on by default
error: struct `S` is never constructed
 --> <anon>:2:8
  |
2 | struct S;
  |        ^
  |
note: the lint level is defined here
 --> <anon>:1:8
  |
1 | #[deny(dead_code)]
  |        ^^^^^^^^^

However, in the latter two cases, we get a message that is useful, but one needs to manually replace the minuses with underscores (and there may be a few in other cases):

error: struct `S` is never constructed
 --> <anon>:1:8
  |
1 | struct S;
  |        ^
  |
  = note: requested on the command line with `-D dead-code`
error: struct `S` is never constructed
 --> <anon>:1:8
  |
1 | struct S;
  |        ^
  |
  = note: `-D dead-code` implied by `-D warnings`

(By the way, note that the diagnostics also seem to print minus signs even if one used underscores in the command line, like here, i.e. -Ddead_code -- see rust-lang/rust-clippy#11332 as well).

For newcomers, it may make it harder to understand that one can allow(...) things locally with an attribute, plus it may not be obvious that they need to replace the minus signs, even if they tried:

error: expected one of `(`, `,`, `::`, or `=`, found `-`
 --> <anon>:1:13
  |
1 | #[allow(dead-code)]
  |             ^ expected one of `(`, `,`, `::`, or `=`

And, for experienced developers, it is anyway a bit annoying to have to do the replacement all the time.

I don't know what is the best way to improve this. Perhaps an extra help: line would not hurt, and in principle, it could be given in all cases:

error: struct `S` is never constructed
 --> <anon>:1:8
  |
1 | struct S;
  |        ^
  |
  = note: `-D dead-code` implied by `-D warnings`
  = help: add an `#[allow(dead_code)]` attribute to allow

This follows the pattern for the suggestion when using unstable features:

error[E0658]: use of unstable library feature 'num_midpoint'
 --> <source>:2:5
  |
2 |     i32::midpoint(0, 2)
  |     ^^^^^^^^^^^^^
  |
  = note: see issue #110840 <https://github.com/rust-lang/rust/issues/110840> for more information
  = help: add `#![feature(num_midpoint)]` to the crate attributes to enable

Of course, this could potentially encourage users to allow things they should not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintArea: Lints (warnings about flaws in source code) such as unused_mut.D-papercutDiagnostics: An error or lint that needs small tweaks.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