Make MISSING_UNSAFE_ON_EXTERN lint emit future compat info with suggestion to prepend unsafe

This commit is contained in:
Santiago Pastorino 2024-06-03 22:27:57 -03:00
parent 0380321e78
commit 1afc7d716c
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
10 changed files with 40 additions and 16 deletions

View File

@ -177,6 +177,8 @@ ast_passes_match_arm_with_no_body =
`match` arm with no body `match` arm with no body
.suggestion = add a body after the pattern .suggestion = add a body after the pattern
ast_passes_missing_unsafe_on_extern = extern blocks must be unsafe
ast_passes_module_nonascii = trying to load file for module `{$name}` with non-ascii identifier name ast_passes_module_nonascii = trying to load file for module `{$name}` with non-ascii identifier name
.help = consider using the `#[path]` attribute to specify filesystem path .help = consider using the `#[path]` attribute to specify filesystem path

View File

@ -1044,12 +1044,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
if this.features.unsafe_extern_blocks { if this.features.unsafe_extern_blocks {
if &Safety::Default == safety { if &Safety::Default == safety {
this.lint_buffer.buffer_lint( if item.span.at_least_rust_2024() {
MISSING_UNSAFE_ON_EXTERN, this.dcx()
item.id, .emit_err(errors::MissingUnsafeOnExtern { span: item.span });
item.span, } else {
BuiltinLintDiag::MissingUnsafeOnExtern, this.lint_buffer.buffer_lint(
); MISSING_UNSAFE_ON_EXTERN,
item.id,
item.span,
BuiltinLintDiag::MissingUnsafeOnExtern {
suggestion: item.span.shrink_to_lo(),
},
);
}
} }
} else if let &Safety::Unsafe(span) = safety { } else if let &Safety::Unsafe(span) = safety {
this.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" }); this.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" });

View File

@ -494,6 +494,13 @@ pub struct UnsafeItem {
pub kind: &'static str, pub kind: &'static str,
} }
#[derive(Diagnostic)]
#[diag(ast_passes_missing_unsafe_on_extern)]
pub struct MissingUnsafeOnExtern {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(ast_passes_fieldless_union)] #[diag(ast_passes_fieldless_union)]
pub struct FieldlessUnion { pub struct FieldlessUnion {

View File

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

View File

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

View File

@ -2740,7 +2740,10 @@ pub enum DeprecatedWhereClauseLocationSugg {
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_missing_unsafe_on_extern)] #[diag(lint_missing_unsafe_on_extern)]
pub struct MissingUnsafeOnExtern; pub struct MissingUnsafeOnExtern {
#[suggestion(code = "unsafe ", applicability = "machine-applicable")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_single_use_lifetime)] #[diag(lint_single_use_lifetime)]

View File

@ -4858,8 +4858,9 @@ declare_lint! {
/// ///
/// ### Example /// ### Example
/// ///
/// ```rust,edition2024,ignore /// ```rust
/// #![feature(unsafe_extern_blocks)] /// #![feature(unsafe_extern_blocks)]
/// #![warn(missing_unsafe_on_extern)]
/// #![allow(dead_code)] /// #![allow(dead_code)]
/// ///
/// extern "C" { /// extern "C" {
@ -4883,5 +4884,8 @@ declare_lint! {
pub MISSING_UNSAFE_ON_EXTERN, pub MISSING_UNSAFE_ON_EXTERN,
Allow, Allow,
"detects missing unsafe keyword on extern declarations", "detects missing unsafe keyword on extern declarations",
@edition Edition2024 => Deny; @future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
reference: "issue #123743 <https://github.com/rust-lang/rust/issues/123743>",
};
} }

View File

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

View File

@ -1,4 +1,4 @@
error: extern blocks should be unsafe error: extern blocks must be unsafe
--> $DIR/extern-items.rs:9:1 --> $DIR/extern-items.rs:9:1
| |
LL | / extern "C" { LL | / extern "C" {
@ -7,8 +7,6 @@ LL | | static TEST1: i32;
LL | | fn test1(i: i32); LL | | fn test1(i: i32);
LL | | } LL | | }
| |_^ | |_^
|
= note: `#[deny(missing_unsafe_on_extern)]` on by default
error: aborting due to 1 previous error error: aborting due to 1 previous error

View File

@ -7,7 +7,7 @@
#![feature(unsafe_extern_blocks)] #![feature(unsafe_extern_blocks)]
extern "C" { extern "C" {
//[edition2024]~^ ERROR extern blocks should be unsafe //[edition2024]~^ ERROR extern blocks must be unsafe
static TEST1: i32; static TEST1: i32;
fn test1(i: i32); fn test1(i: i32);
} }