Rollup merge of #121560 - Noratrieb:stop-lint-macro-nonsense, r=jieyouxu

Allow `#[deny]` inside `#[forbid]` as a no-op

Forbid cannot be overriden. When someome tries to do this anyways, it results in a hard error. That makes sense.

Except it doesn't, because macros. Macros may reasonably use `#[deny]` (or `#[warn]` for an allow-by-default lint) in their expansion to assert that their expanded code follows the lint. This is doesn't work when the output gets expanded into a `forbid()` context. This is pretty silly, since both the macros and the code agree on the lint!

By making it a warning instead, we remove the problem with the macro, which is now nothing as warnings are suppressed in macro expanded code, while still telling users that something is up.

fixes #121483
This commit is contained in:
Matthias Krüger 2024-10-20 16:54:08 +02:00 committed by GitHub
commit 7b714d4735
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 260 additions and 32 deletions

View file

@ -493,7 +493,10 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
//
// This means that this only errors if we're truly lowering the lint
// level from forbid.
if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid {
if self.lint_added_lints && level == Level::Deny && old_level == Level::Forbid {
// Having a deny inside a forbid is fine and is ignored, so we skip this check.
return;
} else if self.lint_added_lints && level != Level::Forbid && old_level == Level::Forbid {
// Backwards compatibility check:
//
// We used to not consider `forbid(lint_group)`

View file

@ -156,7 +156,7 @@ declare_lint! {
///
/// ```rust
/// #![forbid(warnings)]
/// #![deny(bad_style)]
/// #![warn(bad_style)]
///
/// fn main() {}
/// ```

View file

@ -2758,7 +2758,6 @@ ui/lint/issue-63364.rs
ui/lint/issue-70819-dont-override-forbid-in-same-scope.rs
ui/lint/issue-79546-fuel-ice.rs
ui/lint/issue-79744.rs
ui/lint/issue-80988.rs
ui/lint/issue-81218.rs
ui/lint/issue-83477.rs
ui/lint/issue-87274-paren-parent.rs

View file

@ -0,0 +1,7 @@
#[macro_export]
macro_rules! emit_allow {
() => {
#[allow(unsafe_code)]
let _so_safe = 0;
};
}

View file

@ -0,0 +1,7 @@
#[macro_export]
macro_rules! emit_deny {
() => {
#[deny(unsafe_code)]
let _so_safe = 0;
};
}

View file

@ -0,0 +1,7 @@
#[macro_export]
macro_rules! emit_forbid {
() => {
#[forbid(unsafe_code)]
let _so_safe = 0;
};
}

View file

@ -0,0 +1,7 @@
#[macro_export]
macro_rules! emit_warn {
() => {
#[warn(unsafe_code)]
let _so_safe = 0;
};
}

View file

@ -0,0 +1,35 @@
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: usage of an `unsafe` block
--> $DIR/deny-inside-forbid-ignored.rs:16:13
|
LL | unsafe { /* ≽^•⩊•^≼ */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/deny-inside-forbid-ignored.rs:8:10
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ^^^^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0453`.

View file

@ -0,0 +1,35 @@
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: usage of an `unsafe` block
--> $DIR/deny-inside-forbid-ignored.rs:16:13
|
LL | unsafe { /* ≽^•⩊•^≼ */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/deny-inside-forbid-ignored.rs:8:10
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ^^^^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0453`.

View file

@ -0,0 +1,20 @@
//! Ensure that using deny inside forbid is treated as a no-op, and does not override the level to
//! deny.
//@ revisions: source_only cli_forbid cli_forbid_warnings
//@[cli_forbid] compile-flags: -F unsafe_code
//@[cli_forbid_warnings] compile-flags: -F warnings
#[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
fn main() {
#[deny(unsafe_code)] // m-m-maybe we can have unsafe code in here?
{
#[allow(unsafe_code)] // let's have some unsafe code in here
//~^ ERROR allow(unsafe_code) incompatible with previous forbid
//~| ERROR allow(unsafe_code) incompatible with previous forbid
{
unsafe { /* ≽^•⩊•^≼ */ }
//~^ ERROR usage of an `unsafe` block
}
}
}

View file

@ -0,0 +1,35 @@
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/deny-inside-forbid-ignored.rs:12:17
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ----------- `forbid` level set here
...
LL | #[allow(unsafe_code)] // let's have some unsafe code in here
| ^^^^^^^^^^^ overruled by previous forbid
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: usage of an `unsafe` block
--> $DIR/deny-inside-forbid-ignored.rs:16:13
|
LL | unsafe { /* ≽^•⩊•^≼ */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/deny-inside-forbid-ignored.rs:8:10
|
LL | #[forbid(unsafe_code)] // NO UNSAFE CODE IN HERE!!
| ^^^^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0453`.

View file

@ -0,0 +1,26 @@
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/forbid-macro-with-deny.rs:39:5
|
LL | #![forbid(unsafe_code)]
| ----------- `forbid` level set here
...
LL | allow_macro::emit_allow! {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= note: this error originates in the macro `allow_macro::emit_allow` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0453]: allow(unsafe_code) incompatible with previous forbid
--> $DIR/forbid-macro-with-deny.rs:39:5
|
LL | #![forbid(unsafe_code)]
| ----------- `forbid` level set here
...
LL | allow_macro::emit_allow! {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
= note: this error originates in the macro `allow_macro::emit_allow` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0453`.

View file

@ -0,0 +1,45 @@
//! Ensure that when a macro (or normal code) does `#[deny]` inside a `#[forbid]` context, no error
//! is emitted, as both parties agree on the treatment of the lint.
//!
//! However, still emit an error if the macro does `#[allow]` or `#[warn]`.
//@ revisions: forbid deny warn allow
//@[forbid] aux-build:forbid-macro.rs
//@[deny] aux-build:deny-macro.rs
//@[warn] aux-build:warn-macro.rs
//@[allow] aux-build:allow-macro.rs
//@[forbid] check-pass
//@[deny] check-pass
#![forbid(unsafe_code)]
#[cfg(allow)]
extern crate allow_macro;
#[cfg(deny)]
extern crate deny_macro;
#[cfg(forbid)]
extern crate forbid_macro;
#[cfg(warn)]
extern crate warn_macro;
fn main() {
#[cfg(forbid)]
forbid_macro::emit_forbid! {} // OK
#[cfg(deny)]
deny_macro::emit_deny! {} // OK
#[cfg(warn)]
warn_macro::emit_warn! {}
//[warn]~^ ERROR warn(unsafe_code) incompatible with previous forbid
//[warn]~| ERROR warn(unsafe_code) incompatible with previous forbid
#[cfg(allow)]
allow_macro::emit_allow! {}
//[allow]~^ ERROR allow(unsafe_code) incompatible with previous forbid
//[allow]~| ERROR allow(unsafe_code) incompatible with previous forbid
#[deny(unsafe_code)] // OK
let _ = 0;
}

View file

@ -0,0 +1,26 @@
error[E0453]: warn(unsafe_code) incompatible with previous forbid
--> $DIR/forbid-macro-with-deny.rs:34:5
|
LL | #![forbid(unsafe_code)]
| ----------- `forbid` level set here
...
LL | warn_macro::emit_warn! {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= note: this error originates in the macro `warn_macro::emit_warn` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0453]: warn(unsafe_code) incompatible with previous forbid
--> $DIR/forbid-macro-with-deny.rs:34:5
|
LL | #![forbid(unsafe_code)]
| ----------- `forbid` level set here
...
LL | warn_macro::emit_warn! {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ overruled by previous forbid
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
= note: this error originates in the macro `warn_macro::emit_warn` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0453`.

View file

@ -19,9 +19,9 @@
fn forbid_first(num: i32) -> i32 {
#![forbid(unused)]
#![deny(unused)]
//~^ ERROR: deny(unused) incompatible with previous forbid
//~| WARNING being phased out
#![warn(unused)]
//~^ ERROR: warn(unused) incompatible with previous forbid
//~| WARNING being phased out
#![allow(unused)]
num * num

View file

@ -1,9 +1,10 @@
error: deny(unused) incompatible with previous forbid
--> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:21:13
error: warn(unused) incompatible with previous forbid
--> $DIR/issue-70819-dont-override-forbid-in-same-scope.rs:22:13
|
LL | #![forbid(unused)]
| ------ `forbid` level set here
LL | #![deny(unused)]
LL | #![warn(unused)]
| ^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

View file

@ -1,10 +0,0 @@
// Regression test for #80988
//
//@ check-pass
#![forbid(warnings)]
#[deny(warnings)]
//~^ WARNING incompatible with previous forbid
//~| WARNING being phased out
fn main() {}

View file

@ -1,15 +0,0 @@
warning: deny(warnings) incompatible with previous forbid
--> $DIR/issue-80988.rs:7:8
|
LL | #![forbid(warnings)]
| -------- `forbid` level set here
LL |
LL | #[deny(warnings)]
| ^^^^^^^^ overruled by previous forbid
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #81670 <https://github.com/rust-lang/rust/issues/81670>
= note: `#[warn(forbidden_lint_groups)]` on by default
warning: 1 warning emitted