Skip to content
Draft
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
15 changes: 12 additions & 3 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,20 @@ impl EarlyLintPass for UnsafeCode {
self.report_unsafe(cx, it.span, BuiltinUnsafe::GlobalAsm);
}

ast::ItemKind::ForeignMod(ForeignMod { safety, .. }) => {
if let Safety::Unsafe(_) = safety {
ast::ItemKind::ForeignMod(ForeignMod { safety, .. }) => match safety {
Safety::Unsafe(_) => {
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeExternBlock);
}
}
Safety::Default => {
// Before edition 2024, the `unsafe` token is optional, but it's still unsafe
// either way
self.report_unsafe(cx, it.span, BuiltinUnsafe::BareExternBlock);
}
Safety::Safe(_) => {
// This should be a syntax error on all editions, but in any case, if it's
// explicitly safe then it's not unsafe code
}
},

ast::ItemKind::MacroDef(..) => {
if let Some(rustc_attr_ir::Attribute::Parsed(AttributeKind::AllowInternalUnsafe(
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ pub(crate) enum BuiltinUnsafe {
UnsafeBlock,
#[diag("usage of an `unsafe extern` block")]
UnsafeExternBlock,
#[diag("usage of an `extern` block")]
#[note(r#"this is unsafe in all editions, even though `unsafe` only became required in the 2024 edition"#)]
BareExternBlock,
#[diag("declaration of an `unsafe` trait")]
UnsafeTrait,
#[diag("implementation of an `unsafe` trait")]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/lint-unsafe-code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl NakedTrait for Naked {
//~^ ERROR usage of the unsafe `naked` attribute
}

extern "C" {
extern "C" { //~ ERROR usage of an `extern` block
#[unsafe(ffi_pure)]
//~^ ERROR usage of the unsafe `ffi_pure` attribute
fn ffi_pure();
Expand Down
16 changes: 15 additions & 1 deletion tests/ui/lint/lint-unsafe-code.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ LL | unsafe {}
LL | unsafe_in_macro!()
| ------------------ in this macro invocation

error: usage of an `extern` block
--> $DIR/lint-unsafe-code.rs:158:1
|
LL | / extern "C" {
LL | | #[unsafe(ffi_pure)]
LL | |
LL | | fn ffi_pure();
... |
LL | | fn ffi_const();
LL | | }
| |_^
|
= note: this is unsafe in all editions, even though `unsafe` only became required in the 2024 edition

error: usage of the unsafe `no_mangle` attribute
--> $DIR/lint-unsafe-code.rs:38:3
|
Expand Down Expand Up @@ -305,5 +319,5 @@ LL | #[unsafe(ffi_const)]
|
= note: `#[ffi_const]` functions shall have no effects except for its return value, which can only depend on the values of the function parameters, and is not affected by changes to the observable state of the program.

error: aborting due to 38 previous errors
error: aborting due to 39 previous errors

14 changes: 14 additions & 0 deletions tests/ui/lint/unsafe_code/extern-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ edition:2021
#![deny(unsafe_code)]

#[allow(unsafe_code)]
extern "C" {
fn foo();
}

extern "C" {
//~^ ERROR usage of an `extern` block [unsafe_code]
fn bar();
}

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/lint/unsafe_code/extern-block.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: usage of an `extern` block
--> $DIR/extern-block.rs:9:1
|
LL | / extern "C" {
LL | |
LL | | fn bar();
LL | | }
| |_^
|
= note: this is unsafe in all editions, even though `unsafe` only became required in the 2024 edition
note: the lint level is defined here
--> $DIR/extern-block.rs:2:9
|
LL | #![deny(unsafe_code)]
| ^^^^^^^^^^^

error: aborting due to 1 previous error

Loading