Migrate "unused unsafe" lint
This commit is contained in:
parent
64f3e4f195
commit
71a9cb9b7e
@ -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
|
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
|
.note = can only be called if the required target features are available
|
||||||
.label = call to function with `#[target_feature]`
|
.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
|
||||||
|
@ -45,7 +45,9 @@ fn in_safety_context(&mut self, safety_context: SafetyContext, f: impl FnOnce(&m
|
|||||||
self.warn_unused_unsafe(
|
self.warn_unused_unsafe(
|
||||||
hir_id,
|
hir_id,
|
||||||
block_span,
|
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);
|
f(self);
|
||||||
} else {
|
} else {
|
||||||
@ -59,7 +61,9 @@ fn in_safety_context(&mut self, safety_context: SafetyContext, f: impl FnOnce(&m
|
|||||||
hir_id,
|
hir_id,
|
||||||
span,
|
span,
|
||||||
if self.unsafe_op_in_unsafe_fn_allowed() {
|
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 {
|
} else {
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
@ -95,17 +99,15 @@ fn warn_unused_unsafe(
|
|||||||
&self,
|
&self,
|
||||||
hir_id: hir::HirId,
|
hir_id: hir::HirId,
|
||||||
block_span: Span,
|
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 block_span = self.tcx.sess.source_map().guess_head_span(block_span);
|
||||||
let msg = "unnecessary `unsafe` block";
|
self.tcx.emit_spanned_lint(
|
||||||
self.tcx.struct_span_lint_hir(UNUSED_UNSAFE, hir_id, block_span, msg, |lint| {
|
UNUSED_UNSAFE,
|
||||||
lint.span_label(block_span, msg);
|
hir_id,
|
||||||
if let Some((span, kind)) = enclosing_unsafe {
|
block_span,
|
||||||
lint.span_label(span, format!("because it's nested under this `unsafe` {}", kind));
|
UnusedUnsafe { span: block_span, enclosing: enclosing_unsafe },
|
||||||
}
|
);
|
||||||
lint
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether the `unsafe_op_in_unsafe_fn` lint is `allow`ed at the current HIR node.
|
/// Whether the `unsafe_op_in_unsafe_fn` lint is `allow`ed at the current HIR node.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use rustc_macros::{LintDiagnostic, SessionDiagnostic};
|
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
@ -313,3 +313,26 @@ pub struct CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed<'a> {
|
|||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub function: &'a str,
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user