fix [undocumented_unsafe_blocks] FP with trait/impl items
This commit is contained in:
parent
1419ac2982
commit
d395646a60
4 changed files with 171 additions and 36 deletions
|
|
@ -350,12 +350,13 @@ fn block_parents_have_safety_comment(
|
|||
let (span, hir_id) = match cx.tcx.parent_hir_node(id) {
|
||||
Node::Expr(expr) => match cx.tcx.parent_hir_node(expr.hir_id) {
|
||||
Node::LetStmt(hir::LetStmt { span, hir_id, .. }) => (*span, *hir_id),
|
||||
Node::Item(hir::Item {
|
||||
kind: ItemKind::Const(..) | ItemKind::Static(..),
|
||||
span,
|
||||
owner_id,
|
||||
..
|
||||
}) => (*span, cx.tcx.local_def_id_to_hir_id(owner_id.def_id)),
|
||||
|
||||
node if let Some((span, hir_id)) = span_and_hid_of_item_alike_node(&node)
|
||||
&& is_const_or_static(&node) =>
|
||||
{
|
||||
(span, hir_id)
|
||||
},
|
||||
|
||||
_ => {
|
||||
if is_branchy(expr) {
|
||||
return false;
|
||||
|
|
@ -371,12 +372,13 @@ fn block_parents_have_safety_comment(
|
|||
..
|
||||
})
|
||||
| Node::LetStmt(hir::LetStmt { span, hir_id, .. }) => (*span, *hir_id),
|
||||
Node::Item(hir::Item {
|
||||
kind: ItemKind::Const(..) | ItemKind::Static(..),
|
||||
span,
|
||||
owner_id,
|
||||
..
|
||||
}) => (*span, cx.tcx.local_def_id_to_hir_id(owner_id.def_id)),
|
||||
|
||||
node if let Some((span, hir_id)) = span_and_hid_of_item_alike_node(&node)
|
||||
&& is_const_or_static(&node) =>
|
||||
{
|
||||
(span, hir_id)
|
||||
},
|
||||
|
||||
_ => return false,
|
||||
};
|
||||
// if unsafe block is part of a let/const/static statement,
|
||||
|
|
@ -604,31 +606,18 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
|
|||
|
||||
fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
|
||||
let body = cx.enclosing_body?;
|
||||
let mut span = cx.tcx.hir_body(body).value.span;
|
||||
let mut maybe_global_var = false;
|
||||
for (_, node) in cx.tcx.hir_parent_iter(body.hir_id) {
|
||||
match node {
|
||||
Node::Expr(e) => span = e.span,
|
||||
Node::Block(_) | Node::Arm(_) | Node::Stmt(_) | Node::LetStmt(_) => (),
|
||||
Node::Item(hir::Item {
|
||||
kind: ItemKind::Const(..) | ItemKind::Static(..),
|
||||
..
|
||||
}) => maybe_global_var = true,
|
||||
Node::Item(hir::Item {
|
||||
kind: ItemKind::Mod(_),
|
||||
span: item_span,
|
||||
..
|
||||
}) => {
|
||||
span = *item_span;
|
||||
break;
|
||||
for (_, parent_node) in cx.tcx.hir_parent_iter(body.hir_id) {
|
||||
match parent_node {
|
||||
Node::Crate(mod_) => return Some(mod_.spans.inner_span),
|
||||
node if let Some((span, _)) = span_and_hid_of_item_alike_node(&node)
|
||||
&& !is_const_or_static(&node) =>
|
||||
{
|
||||
return Some(span);
|
||||
},
|
||||
Node::Crate(mod_) if maybe_global_var => {
|
||||
span = mod_.spans.inner_span;
|
||||
},
|
||||
_ => break,
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
Some(span)
|
||||
None
|
||||
}
|
||||
|
||||
fn span_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
|
||||
|
|
@ -717,3 +706,28 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn span_and_hid_of_item_alike_node(node: &Node<'_>) -> Option<(Span, HirId)> {
|
||||
match node {
|
||||
Node::Item(item) => Some((item.span, item.owner_id.into())),
|
||||
Node::TraitItem(ti) => Some((ti.span, ti.owner_id.into())),
|
||||
Node::ImplItem(ii) => Some((ii.span, ii.owner_id.into())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_const_or_static(node: &Node<'_>) -> bool {
|
||||
matches!(
|
||||
node,
|
||||
Node::Item(hir::Item {
|
||||
kind: ItemKind::Const(..) | ItemKind::Static(..),
|
||||
..
|
||||
}) | Node::ImplItem(hir::ImplItem {
|
||||
kind: hir::ImplItemKind::Const(..),
|
||||
..
|
||||
}) | Node::TraitItem(hir::TraitItem {
|
||||
kind: hir::TraitItemKind::Const(..),
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue