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

Fire unsafe_code lint on unsafe extern blocks #127535

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ lint_builtin_unreachable_pub = unreachable `pub` {$what}

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_trait = declaration of an `unsafe` trait
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ impl EarlyLintPass for UnsafeCode {
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);
}
}

_ => {}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub enum BuiltinUnsafe {
AllowInternalUnsafe,
#[diag(lint_builtin_unsafe_block)]
UnsafeBlock,
#[diag(lint_builtin_unsafe_extern_block)]
UnsafeExternBlock,
#[diag(lint_builtin_unsafe_trait)]
UnsafeTrait,
#[diag(lint_builtin_unsafe_impl)]
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/lint/unsafe_code/unsafe-extern-blocks.rs
Original file line number Diff line number Diff line change
@@ -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() {}
17 changes: 17 additions & 0 deletions tests/ui/lint/unsafe_code/unsafe-extern-blocks.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading