Skip to content

Commit b185de1

Browse files
spastorinolcnr
authored andcommitted
Make MISSING_UNSAFE_ON_EXTERN lint emit future compat info with suggestion to prepend unsafe
1 parent 00e36a4 commit b185de1

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 2 additions & 0 deletions
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

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 13 additions & 6 deletions
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" });

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
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 {

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 0 deletions
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

compiler/rustc_lint/src/context/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
202202
};
203203
lints::DeprecatedWhereClauseLocation { suggestion }.decorate_lint(diag);
204204
}
205-
BuiltinLintDiag::MissingUnsafeOnExtern => {
206-
lints::MissingUnsafeOnExtern.decorate_lint(diag);
205+
BuiltinLintDiag::MissingUnsafeOnExtern { suggestion } => {
206+
lints::MissingUnsafeOnExtern { suggestion }.decorate_lint(diag);
207207
}
208208
BuiltinLintDiag::SingleUseLifetime {
209209
param_span,

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,10 @@ pub enum DeprecatedWhereClauseLocationSugg {
27322732

27332733
#[derive(LintDiagnostic)]
27342734
#[diag(lint_missing_unsafe_on_extern)]
2735-
pub struct MissingUnsafeOnExtern;
2735+
pub struct MissingUnsafeOnExtern {
2736+
#[suggestion(code = "unsafe ", applicability = "machine-applicable")]
2737+
pub suggestion: Span,
2738+
}
27362739

27372740
#[derive(LintDiagnostic)]
27382741
#[diag(lint_single_use_lifetime)]

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,8 +4810,9 @@ declare_lint! {
48104810
///
48114811
/// ### Example
48124812
///
4813-
/// ```rust,edition2024,ignore
4813+
/// ```rust
48144814
/// #![feature(unsafe_extern_blocks)]
4815+
/// #![warn(missing_unsafe_on_extern)]
48154816
/// #![allow(dead_code)]
48164817
///
48174818
/// extern "C" {
@@ -4835,5 +4836,8 @@ declare_lint! {
48354836
pub MISSING_UNSAFE_ON_EXTERN,
48364837
Allow,
48374838
"detects missing unsafe keyword on extern declarations",
4838-
@edition Edition2024 => Deny;
4839+
@future_incompatible = FutureIncompatibleInfo {
4840+
reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
4841+
reference: "issue #123743 <https://github.com/rust-lang/rust/issues/123743>",
4842+
};
48394843
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ pub enum BuiltinLintDiag {
626626
UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),
627627
UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>),
628628
DeprecatedWhereclauseLocation(Span, Option<(Span, String)>),
629-
MissingUnsafeOnExtern,
629+
MissingUnsafeOnExtern {
630+
suggestion: Span,
631+
},
630632
SingleUseLifetime {
631633
/// Span of the parameter which declares this lifetime.
632634
param_span: Span,
Lines changed: 1 addition & 3 deletions
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

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

Lines changed: 1 addition & 1 deletion
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)