From 6db9132ad3267599caf083b48f349852bbbbd434 Mon Sep 17 00:00:00 2001 From: funsafemath Date: Thu, 6 Aug 2026 05:20:55 +0100 Subject: [PATCH] Add a suggestion to MissingUnsafeOnExtern diagnostic --- compiler/rustc_ast_passes/src/ast_validation.rs | 5 ++++- compiler/rustc_ast_passes/src/diagnostics.rs | 7 +++++++ .../unsafe-extern-blocks/extern-items.edition2024.stderr | 6 +++++- ...afe-unsafe-on-unadorned-extern-block.edition2024.stderr | 6 +++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index c22b517b3ddf9..d1368d8f9f633 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1531,7 +1531,10 @@ impl Visitor<'_> for AstValidator<'_> { if &Safety::Default == safety { if item.span.at_least_rust_2024() { - self.dcx().emit_err(diagnostics::MissingUnsafeOnExtern { span: item.span }); + self.dcx().emit_err(diagnostics::MissingUnsafeOnExtern { + span: item.span, + unsafe_span: item.span.shrink_to_lo(), + }); } else { self.lint_buffer.buffer_lint( MISSING_UNSAFE_ON_EXTERN, diff --git a/compiler/rustc_ast_passes/src/diagnostics.rs b/compiler/rustc_ast_passes/src/diagnostics.rs index db006e50aaa31..0814c79d339bd 100644 --- a/compiler/rustc_ast_passes/src/diagnostics.rs +++ b/compiler/rustc_ast_passes/src/diagnostics.rs @@ -737,6 +737,13 @@ pub(crate) struct UnsafeItem { pub(crate) struct MissingUnsafeOnExtern { #[primary_span] pub span: Span, + + #[suggestion( + "needs `unsafe` before the extern keyword", + code = "unsafe ", + applicability = "machine-applicable" + )] + pub unsafe_span: Span, } #[derive(Diagnostic)] diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/extern-items.edition2024.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/extern-items.edition2024.stderr index 17b49d8ed5c36..16b3af0feadeb 100644 --- a/tests/ui/rust-2024/unsafe-extern-blocks/extern-items.edition2024.stderr +++ b/tests/ui/rust-2024/unsafe-extern-blocks/extern-items.edition2024.stderr @@ -1,7 +1,11 @@ error: extern blocks must be unsafe --> $DIR/extern-items.rs:6:1 | -LL | / extern "C" { +LL | extern "C" { + | ^ + | | + | _help: needs `unsafe` before the extern keyword: `unsafe` + | | LL | | LL | | static TEST1: i32; LL | | fn test1(i: i32); diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr index 874b32346af17..91f9a8611e508 100644 --- a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr +++ b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr @@ -1,7 +1,11 @@ error: extern blocks must be unsafe --> $DIR/safe-unsafe-on-unadorned-extern-block.rs:5:1 | -LL | / extern "C" { +LL | extern "C" { + | ^ + | | + | _help: needs `unsafe` before the extern keyword: `unsafe` + | | LL | | LL | | safe static TEST1: i32; ... |