Skip to content

Commit 1afc7d7

Browse files
committed
Make MISSING_UNSAFE_ON_EXTERN lint emit future compat info with suggestion to prepend unsafe
1 parent 0380321 commit 1afc7d7

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

Diff for: compiler/rustc_ast_passes/messages.ftl

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

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,19 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10441044

10451045
if this.features.unsafe_extern_blocks {
10461046
if &Safety::Default == safety {
1047-
this.lint_buffer.buffer_lint(
1048-
MISSING_UNSAFE_ON_EXTERN,
1049-
item.id,
1050-
item.span,
1051-
BuiltinLintDiag::MissingUnsafeOnExtern,
1052-
);
1047+
if item.span.at_least_rust_2024() {
1048+
this.dcx()
1049+
.emit_err(errors::MissingUnsafeOnExtern { span: item.span });
1050+
} else {
1051+
this.lint_buffer.buffer_lint(
1052+
MISSING_UNSAFE_ON_EXTERN,
1053+
item.id,
1054+
item.span,
1055+
BuiltinLintDiag::MissingUnsafeOnExtern {
1056+
suggestion: item.span.shrink_to_lo(),
1057+
},
1058+
);
1059+
}
10531060
}
10541061
} else if let &Safety::Unsafe(span) = safety {
10551062
this.dcx().emit_err(errors::UnsafeItem { span, kind: "extern block" });

Diff for: compiler/rustc_ast_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ pub struct UnsafeItem {
494494
pub kind: &'static str,
495495
}
496496

497+
#[derive(Diagnostic)]
498+
#[diag(ast_passes_missing_unsafe_on_extern)]
499+
pub struct MissingUnsafeOnExtern {
500+
#[primary_span]
501+
pub span: Span,
502+
}
503+
497504
#[derive(Diagnostic)]
498505
#[diag(ast_passes_fieldless_union)]
499506
pub struct FieldlessUnion {

Diff for: compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ lint_metavariable_wrong_operator = meta-variable repeats with different Kleene o
463463
lint_missing_fragment_specifier = missing fragment specifier
464464
465465
lint_missing_unsafe_on_extern = extern blocks should be unsafe
466+
.suggestion = needs `unsafe` before the extern keyword
466467
467468
lint_mixed_script_confusables =
468469
the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables

Diff for: compiler/rustc_lint/src/context/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
205205
};
206206
lints::DeprecatedWhereClauseLocation { suggestion }.decorate_lint(diag);
207207
}
208-
BuiltinLintDiag::MissingUnsafeOnExtern => {
209-
lints::MissingUnsafeOnExtern.decorate_lint(diag);
208+
BuiltinLintDiag::MissingUnsafeOnExtern { suggestion } => {
209+
lints::MissingUnsafeOnExtern { suggestion }.decorate_lint(diag);
210210
}
211211
BuiltinLintDiag::SingleUseLifetime {
212212
param_span,

Diff for: compiler/rustc_lint/src/lints.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,10 @@ pub enum DeprecatedWhereClauseLocationSugg {
27402740

27412741
#[derive(LintDiagnostic)]
27422742
#[diag(lint_missing_unsafe_on_extern)]
2743-
pub struct MissingUnsafeOnExtern;
2743+
pub struct MissingUnsafeOnExtern {
2744+
#[suggestion(code = "unsafe ", applicability = "machine-applicable")]
2745+
pub suggestion: Span,
2746+
}
27442747

27452748
#[derive(LintDiagnostic)]
27462749
#[diag(lint_single_use_lifetime)]

Diff for: compiler/rustc_lint_defs/src/builtin.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4858,8 +4858,9 @@ declare_lint! {
48584858
///
48594859
/// ### Example
48604860
///
4861-
/// ```rust,edition2024,ignore
4861+
/// ```rust
48624862
/// #![feature(unsafe_extern_blocks)]
4863+
/// #![warn(missing_unsafe_on_extern)]
48634864
/// #![allow(dead_code)]
48644865
///
48654866
/// extern "C" {
@@ -4883,5 +4884,8 @@ declare_lint! {
48834884
pub MISSING_UNSAFE_ON_EXTERN,
48844885
Allow,
48854886
"detects missing unsafe keyword on extern declarations",
4886-
@edition Edition2024 => Deny;
4887+
@future_incompatible = FutureIncompatibleInfo {
4888+
reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4889+
reference: "issue #123743 <https://github.com/rust-lang/rust/issues/123743>",
4890+
};
48874891
}

Diff for: compiler/rustc_lint_defs/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,9 @@ pub enum BuiltinLintDiag {
630630
UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),
631631
UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>),
632632
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
633-
MissingUnsafeOnExtern,
633+
MissingUnsafeOnExtern {
634+
suggestion: Span,
635+
},
634636
SingleUseLifetime {
635637
/// Span of the parameter which declares this lifetime.
636638
param_span: Span,
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: extern blocks should be unsafe
1+
error: extern blocks must be unsafe
22
--> $DIR/extern-items.rs:9:1
33
|
44
LL | / extern "C" {
@@ -7,8 +7,6 @@ LL | | static TEST1: i32;
77
LL | | fn test1(i: i32);
88
LL | | }
99
| |_^
10-
|
11-
= note: `#[deny(missing_unsafe_on_extern)]` on by default
1210

1311
error: aborting due to 1 previous error
1412

Diff for: tests/ui/rust-2024/unsafe-extern-blocks/extern-items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![feature(unsafe_extern_blocks)]
88

99
extern "C" {
10-
//[edition2024]~^ ERROR extern blocks should be unsafe
10+
//[edition2024]~^ ERROR extern blocks must be unsafe
1111
static TEST1: i32;
1212
fn test1(i: i32);
1313
}

0 commit comments

Comments
 (0)