Skip to content

Commit 2e0591b

Browse files
Rollup merge of #127535 - spastorino:unsafe_code-unsafe_extern_blocks, r=oli-obk
Fire unsafe_code lint on unsafe extern blocks Fixes #126738
2 parents 38c314e + a3ef94e commit 2e0591b

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ lint_builtin_unreachable_pub = unreachable `pub` {$what}
159159
160160
lint_builtin_unsafe_block = usage of an `unsafe` block
161161
162+
lint_builtin_unsafe_extern_block = usage of an `unsafe extern` block
163+
162164
lint_builtin_unsafe_impl = implementation of an `unsafe` trait
163165
164166
lint_builtin_unsafe_trait = declaration of an `unsafe` trait

compiler/rustc_lint/src/builtin.rs

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ impl EarlyLintPass for UnsafeCode {
325325
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
326326
}
327327

328+
ast::ItemKind::ForeignMod(ForeignMod { safety, .. }) => {
329+
if let Safety::Unsafe(_) = safety {
330+
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeExternBlock);
331+
}
332+
}
333+
328334
_ => {}
329335
}
330336
}

compiler/rustc_lint/src/lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub enum BuiltinUnsafe {
8181
AllowInternalUnsafe,
8282
#[diag(lint_builtin_unsafe_block)]
8383
UnsafeBlock,
84+
#[diag(lint_builtin_unsafe_extern_block)]
85+
UnsafeExternBlock,
8486
#[diag(lint_builtin_unsafe_trait)]
8587
UnsafeTrait,
8688
#[diag(lint_builtin_unsafe_impl)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(unsafe_extern_blocks)]
2+
#![deny(unsafe_code)]
3+
4+
#[allow(unsafe_code)]
5+
unsafe extern "C" {
6+
fn foo();
7+
}
8+
9+
unsafe extern "C" {
10+
//~^ ERROR usage of an `unsafe extern` block [unsafe_code]
11+
fn bar();
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: usage of an `unsafe extern` block
2+
--> $DIR/unsafe-extern-blocks.rs:9:1
3+
|
4+
LL | / unsafe extern "C" {
5+
LL | |
6+
LL | | fn bar();
7+
LL | | }
8+
| |_^
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/unsafe-extern-blocks.rs:2:9
12+
|
13+
LL | #![deny(unsafe_code)]
14+
| ^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+

0 commit comments

Comments
 (0)