Make extern blocks without unsafe warn in edition 2024

This commit is contained in:
Santiago Pastorino 2024-05-21 12:27:21 -03:00
parent 46cd80b691
commit 3ba8de0b60
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
10 changed files with 78 additions and 7 deletions

View File

@ -15,7 +15,8 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_feature::Features;
use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,
PATTERNS_IN_FNS_WITHOUT_BODY,
};
use rustc_session::lint::{BuiltinLintDiag, LintBuffer};
use rustc_session::Session;
@ -1016,12 +1017,22 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
walk_list!(self, visit_attribute, &item.attrs);
return; // Avoid visiting again.
}
ItemKind::ForeignMod(ForeignMod { abi, .. }) => {
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => {
let old_item = mem::replace(&mut self.extern_mod, Some(item));
self.visibility_not_permitted(
&item.vis,
errors::VisibilityNotPermittedNote::IndividualForeignItems,
);
if &Safety::Default == safety {
self.lint_buffer.buffer_lint(
MISSING_UNSAFE_ON_EXTERN,
item.id,
item.span,
BuiltinLintDiag::MissingUnsafeOnExtern,
);
}
if abi.is_none() {
self.maybe_lint_missing_abi(item.span, item.id);
}

View File

@ -462,6 +462,8 @@ lint_metavariable_wrong_operator = meta-variable repeats with different Kleene o
lint_missing_fragment_specifier = missing fragment specifier
lint_missing_unsafe_on_extern = extern blocks should be unsafe
lint_mixed_script_confusables =
the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables
.includes_note = the usage includes {$includes}

View File

@ -205,6 +205,9 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
};
lints::DeprecatedWhereClauseLocation { suggestion }.decorate_lint(diag);
}
BuiltinLintDiag::MissingUnsafeOnExtern => {
lints::MissingUnsafeOnExtern.decorate_lint(diag);
}
BuiltinLintDiag::SingleUseLifetime {
param_span,
use_span: Some((use_span, elide)),

View File

@ -2738,6 +2738,10 @@ pub enum DeprecatedWhereClauseLocationSugg {
},
}
#[derive(LintDiagnostic)]
#[diag(lint_missing_unsafe_on_extern)]
pub struct MissingUnsafeOnExtern;
#[derive(LintDiagnostic)]
#[diag(lint_single_use_lifetime)]
pub struct SingleUseLifetime {

View File

@ -66,6 +66,7 @@ declare_lint_pass! {
META_VARIABLE_MISUSE,
MISSING_ABI,
MISSING_FRAGMENT_SPECIFIER,
MISSING_UNSAFE_ON_EXTERN,
MUST_NOT_SUSPEND,
NAMED_ARGUMENTS_USED_POSITIONALLY,
NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
@ -4851,3 +4852,33 @@ declare_lint! {
reference: "issue #27970 <https://github.com/rust-lang/rust/issues/27970>",
};
}
declare_lint! {
/// The `missing_unsafe_on_extern` lint detects missing unsafe keyword on extern declarations.
///
/// ### Example
///
/// ```rust,edition2024
/// extern "C" {
/// fn foo(_: i32);
/// }
///
/// fn main() {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Declaring extern items, even without ever using them, can cause Undefined Behavior. We
/// should consider all sources of Undefined Behavior to be unsafe.
///
/// This is a [future-incompatible] lint to transition this to a
/// hard error in the future.
///
/// [future-incompatible]: ../index.md#future-incompatible-lints
pub MISSING_UNSAFE_ON_EXTERN,
Allow,
"detects missing unsafe keyword on extern declarations",
@edition Edition2024 => Warn;
}

View File

@ -630,6 +630,7 @@ pub enum BuiltinLintDiag {
UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),
UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>),
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
MissingUnsafeOnExtern,
SingleUseLifetime {
/// Span of the parameter which declares this lifetime.
param_span: Span,

View File

@ -0,0 +1,14 @@
warning: extern blocks should be unsafe
--> $DIR/extern-items.rs:7:1
|
LL | / extern "C" {
LL | |
LL | | static TEST1: i32;
LL | | fn test1(i: i32);
LL | | }
| |_^
|
= note: `#[warn(missing_unsafe_on_extern)]` on by default
warning: 1 warning emitted

View File

@ -1,6 +1,11 @@
//@ build-pass
//@ revisions: edition2021 edition2024
//@[edition2021] edition:2021
//@[edition2024] edition:2024
//@[edition2024] compile-flags: -Zunstable-options
//@ check-pass
extern "C" {
//[edition2024]~^ WARN extern blocks should be unsafe [missing_unsafe_on_extern]
static TEST1: i32;
fn test1(i: i32);
}

View File

@ -473,8 +473,8 @@ mod items {
/// ItemKind::ForeignMod
mod item_foreign_mod {
extern "C++" {}
extern {}
unsafe extern "C++" {}
unsafe extern {}
}
/// ItemKind::GlobalAsm

View File

@ -451,8 +451,8 @@ mod items {
mod item_mod { }
/// ItemKind::ForeignMod
mod item_foreign_mod {
extern "C++" {}
extern {}
unsafe extern "C++" {}
unsafe extern {}
}
/// ItemKind::GlobalAsm
mod item_global_asm {