Migrate "unused unsafe" lint

This commit is contained in:
TheOddGarlic 2022-08-24 20:16:50 +03:00 committed by mejrs
parent 64f3e4f195
commit 71a9cb9b7e
3 changed files with 43 additions and 12 deletions

View File

@ -169,3 +169,9 @@ mir_build_call_to_fn_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
call to function `{$function}` with `#[target_feature]` is unsafe and requires unsafe function or block
.note = can only be called if the required target features are available
.label = call to function with `#[target_feature]`
mir_build_unused_unsafe = unnecessary `unsafe` block
.label = unnecessary `unsafe` block
mir_build_unused_unsafe_enclosing_block_label = because it's nested under this `unsafe` block
mir_build_unused_unsafe_enclosing_fn_label = because it's nested under this `unsafe` fn

View File

@ -45,7 +45,9 @@ fn in_safety_context(&mut self, safety_context: SafetyContext, f: impl FnOnce(&m
self.warn_unused_unsafe(
hir_id,
block_span,
Some((self.tcx.sess.source_map().guess_head_span(enclosing_span), "block")),
Some(UnusedUnsafeEnclosing::Block {
span: self.tcx.sess.source_map().guess_head_span(enclosing_span),
}),
);
f(self);
} else {
@ -59,7 +61,9 @@ fn in_safety_context(&mut self, safety_context: SafetyContext, f: impl FnOnce(&m
hir_id,
span,
if self.unsafe_op_in_unsafe_fn_allowed() {
self.body_unsafety.unsafe_fn_sig_span().map(|span| (span, "fn"))
self.body_unsafety
.unsafe_fn_sig_span()
.map(|span| UnusedUnsafeEnclosing::Function { span })
} else {
None
},
@ -95,17 +99,15 @@ fn warn_unused_unsafe(
&self,
hir_id: hir::HirId,
block_span: Span,
enclosing_unsafe: Option<(Span, &'static str)>,
enclosing_unsafe: Option<UnusedUnsafeEnclosing>,
) {
let block_span = self.tcx.sess.source_map().guess_head_span(block_span);
let msg = "unnecessary `unsafe` block";
self.tcx.struct_span_lint_hir(UNUSED_UNSAFE, hir_id, block_span, msg, |lint| {
lint.span_label(block_span, msg);
if let Some((span, kind)) = enclosing_unsafe {
lint.span_label(span, format!("because it's nested under this `unsafe` {}", kind));
}
lint
});
self.tcx.emit_spanned_lint(
UNUSED_UNSAFE,
hir_id,
block_span,
UnusedUnsafe { span: block_span, enclosing: enclosing_unsafe },
);
}
/// Whether the `unsafe_op_in_unsafe_fn` lint is `allow`ed at the current HIR node.

View File

@ -1,4 +1,4 @@
use rustc_macros::{LintDiagnostic, SessionDiagnostic};
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::Span;
#[derive(LintDiagnostic)]
@ -313,3 +313,26 @@ pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
pub span: Span,
pub function: &'a str,
}
#[derive(LintDiagnostic)]
#[diag(mir_build::unused_unsafe)]
pub struct UnusedUnsafe {
#[label]
pub span: Span,
#[subdiagnostic]
pub enclosing: Option<UnusedUnsafeEnclosing>,
}
#[derive(SessionSubdiagnostic)]
pub enum UnusedUnsafeEnclosing {
#[label(mir_build::unused_unsafe_enclosing_block_label)]
Block {
#[primary_span]
span: Span,
},
#[label(mir_build::unused_unsafe_enclosing_fn_label)]
Function {
#[primary_span]
span: Span,
},
}