Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"-A warnings" cannot be overwritten later by #[warn] (but other "-A lint" can) #75668

Open
RalfJung opened this issue Aug 18, 2020 · 6 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.

Comments

@RalfJung
Copy link
Member

RalfJung commented Aug 18, 2020

Given the following code

#![warn(warnings, unused)]

fn main() {
    let x = 13;
}

running rustc -A unused test.rs shows a warning that x is unused as expected (since the module scope takes precedence over the command-line arguments). However, rustc -A warnings prints nothing.

This is caused by the following special handling:

// FIXME: This is not general enough to make the warning lint completely override
// normal diagnostic warnings, since the warning lint can also be denied and changed
// later via the source code.
let warnings_allow = sopts
.lint_opts
.iter()
.filter(|&&(ref key, _)| *key == "warnings")
.map(|&(_, ref level)| *level == lint::Allow)
.last()
.unwrap_or(false);
let cap_lints_allow = sopts.lint_cap.map_or(false, |cap| cap == lint::Allow);
let can_emit_warnings = !(warnings_allow || cap_lints_allow);

The FIXME already describes the problem, but I haven't found an open issue for this.

The special case was introduced by this commit as part of #21248. Looks like it is a hack needed to silence some otherwise unsilenceable warnings -- but I am not sure if the hack is still needed, or if there isn't a better way to do this.

@camelid camelid added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels Oct 20, 2020
@FabianWolff

This comment was marked as off-topic.

@RalfJung

This comment was marked as off-topic.

@RalfJung

This comment was marked as off-topic.

@FabianWolff

This comment was marked as off-topic.

@jyn514
Copy link
Member

jyn514 commented May 24, 2023

there's also the opposite problem: -W warnings doesn't override #![deny(warnings)]:

; cat src/main.rs
#![deny(warnings)]
const C: usize = 1;

fn main() {
    core::mem::discriminant(&1);
}
; rustc src/main.rs
; rustc src/main.rs -Wwarnings                
error: constant `C` is never used
 --> src/main.rs:3:7
  |
3 | const C: usize = 1;
  |       ^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(warnings)]
  |         ^^^^^^^^
  = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]`

error: the return value of `mem::discriminant` is unspecified when called with a non-enum type
 --> src/main.rs:6:5
  |
6 |     core::mem::discriminant(&1);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `i32`, which is not an enum.
 --> src/main.rs:6:29
  |
6 |     core::mem::discriminant(&1);
  |                             ^^
  = note: `#[deny(enum_intrinsics_non_enums)]` on by default

I'd expect this to warn on dead_code (because -Wwarnings overrides the crate attribute) but error on enum_intrinsics_non_enums (because -Wwarnings shouldn't override the default lint level).

@RalfJung
Copy link
Member Author

RalfJung commented May 24, 2023

I'd expect this to warn on dead_code (because -Wwarnings overrides the crate attribute)

That would be inconsistent and a bug. Consider:

#[warn(warnings)]
mod outer {
  // Here, warnings are warnings.

  #[deny(warnings)]
  mod inner {
    // Here, they are errors.
  }
}

Command-line flags are "even more outer" than crate-level attributes, so your situation (-Wwarnings and #![deny(warnings)]) should behave consistently with my code above. And it does.

rustc is mostly consistent with this view I described. The issue here is about a case where it is not consistent.

If you want to fundamentally change that, you should open a separate issue, and it probably has to be an RFC, since this would be a change in intended behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants