Skip to content

Commit a0777d3

Browse files
committed
Migrate "unused unsafe" lint
1 parent eb7b30f commit a0777d3

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,9 @@ mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
169169
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe function or block
170170
.note = can only be called if the required target features are available
171171
.label = call to function with `#[target_feature]`
172+
173+
mir_build_unused_unsafe = unnecessary `unsafe` block
174+
.label = unnecessary `unsafe` block
175+
176+
mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block
177+
mir_build_unused_unsafe_enclosing_fn_label = because it's nested under this `unsafe` fn

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
4545
self.warn_unused_unsafe(
4646
hir_id,
4747
block_span,
48-
Some((self.tcx.sess.source_map().guess_head_span(enclosing_span), "block")),
48+
Some(UnusedUnsafeEnclosing::Block {
49+
span: self.tcx.sess.source_map().guess_head_span(enclosing_span),
50+
}),
4951
);
5052
f(self);
5153
} else {
@@ -59,7 +61,9 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
5961
hir_id,
6062
span,
6163
if self.unsafe_op_in_unsafe_fn_allowed() {
62-
self.body_unsafety.unsafe_fn_sig_span().map(|span| (span, "fn"))
64+
self.body_unsafety
65+
.unsafe_fn_sig_span()
66+
.map(|span| UnusedUnsafeEnclosing::Function { span })
6367
} else {
6468
None
6569
},
@@ -95,18 +99,15 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9599
&self,
96100
hir_id: hir::HirId,
97101
block_span: Span,
98-
enclosing_unsafe: Option<(Span, &'static str)>,
102+
enclosing_unsafe: Option<UnusedUnsafeEnclosing>,
99103
) {
100104
let block_span = self.tcx.sess.source_map().guess_head_span(block_span);
101-
self.tcx.struct_span_lint_hir(UNUSED_UNSAFE, hir_id, block_span, |lint| {
102-
let msg = "unnecessary `unsafe` block";
103-
let mut db = lint.build(msg);
104-
db.span_label(block_span, msg);
105-
if let Some((span, kind)) = enclosing_unsafe {
106-
db.span_label(span, format!("because it's nested under this `unsafe` {}", kind));
107-
}
108-
db.emit();
109-
});
105+
self.tcx.emit_spanned_lint(
106+
UNUSED_UNSAFE,
107+
hir_id,
108+
block_span,
109+
UnusedUnsafe { span: block_span, enclosing: enclosing_unsafe },
110+
);
110111
}
111112

112113
/// Whether the `unsafe_op_in_unsafe_fn` lint is `allow`ed at the current HIR node.

compiler/rustc_mir_build/src/errors.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_macros::{LintDiagnostic, SessionDiagnostic};
1+
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
22
use rustc_span::Span;
33

44
#[derive(LintDiagnostic)]
@@ -313,3 +313,26 @@ pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
313313
pub span: Span,
314314
pub function: &'a str,
315315
}
316+
317+
#[derive(LintDiagnostic)]
318+
#[diag(mir_build::unused_unsafe)]
319+
pub struct UnusedUnsafe {
320+
#[label]
321+
pub span: Span,
322+
#[subdiagnostic]
323+
pub enclosing: Option<UnusedUnsafeEnclosing>,
324+
}
325+
326+
#[derive(SessionSubdiagnostic)]
327+
pub enum UnusedUnsafeEnclosing {
328+
#[label(mir_build::unused_unsafe_enclosing_block_label)]
329+
Block {
330+
#[primary_span]
331+
span: Span,
332+
},
333+
#[label(mir_build::unused_unsafe_enclosing_fn_label)]
334+
Function {
335+
#[primary_span]
336+
span: Span,
337+
},
338+
}

0 commit comments

Comments
 (0)