Rollup merge of #127535 - spastorino:unsafe_code-unsafe_extern_blocks, r=oli-obk

Fire unsafe_code lint on unsafe extern blocks

Fixes #126738
This commit is contained in:
Jubilee 2024-07-12 13:47:08 -07:00 committed by GitHub
commit 2e0591b1e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 0 deletions

View File

@ -159,6 +159,8 @@ lint_builtin_unreachable_pub = unreachable `pub` {$what}
lint_builtin_unsafe_block = usage of an `unsafe` block lint_builtin_unsafe_block = usage of an `unsafe` block
lint_builtin_unsafe_extern_block = usage of an `unsafe extern` block
lint_builtin_unsafe_impl = implementation of an `unsafe` trait lint_builtin_unsafe_impl = implementation of an `unsafe` trait
lint_builtin_unsafe_trait = declaration of an `unsafe` trait lint_builtin_unsafe_trait = declaration of an `unsafe` trait

View File

@ -325,6 +325,12 @@ impl EarlyLintPass for UnsafeCode {
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm); self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
} }
ast::ItemKind::ForeignMod(ForeignMod { safety, .. }) => {
if let Safety::Unsafe(_) = safety {
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeExternBlock);
}
}
_ => {} _ => {}
} }
} }

View File

@ -81,6 +81,8 @@ pub enum BuiltinUnsafe {
AllowInternalUnsafe, AllowInternalUnsafe,
#[diag(lint_builtin_unsafe_block)] #[diag(lint_builtin_unsafe_block)]
UnsafeBlock, UnsafeBlock,
#[diag(lint_builtin_unsafe_extern_block)]
UnsafeExternBlock,
#[diag(lint_builtin_unsafe_trait)] #[diag(lint_builtin_unsafe_trait)]
UnsafeTrait, UnsafeTrait,
#[diag(lint_builtin_unsafe_impl)] #[diag(lint_builtin_unsafe_impl)]

View File

@ -0,0 +1,14 @@
#![feature(unsafe_extern_blocks)]
#![deny(unsafe_code)]
#[allow(unsafe_code)]
unsafe extern "C" {
fn foo();
}
unsafe extern "C" {
//~^ ERROR usage of an `unsafe extern` block [unsafe_code]
fn bar();
}
fn main() {}

View File

@ -0,0 +1,17 @@
error: usage of an `unsafe extern` block
--> $DIR/unsafe-extern-blocks.rs:9:1
|
LL | / unsafe extern "C" {
LL | |
LL | | fn bar();
LL | | }
| |_^
|
note: the lint level is defined here
--> $DIR/unsafe-extern-blocks.rs:2:9
|
LL | #![deny(unsafe_code)]
| ^^^^^^^^^^^
error: aborting due to 1 previous error