Skip to content

Commit bcf856b

Browse files
committed
Auto merge of #11375 - J-ZhengLi:issue11246, r=Centri3
fix fp when [`undocumented_unsafe_blocks`] not able to detect comment on globally defined const/static variables fixes: #11246 changelog: fix detection on global variables for [`undocumented_unsafe_blocks`]
2 parents da882f0 + 6eec4a3 commit bcf856b

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -341,44 +341,21 @@ fn block_parents_have_safety_comment(
341341
id: hir::HirId,
342342
) -> bool {
343343
if let Some(node) = get_parent_node(cx.tcx, id) {
344-
return match node {
345-
Node::Expr(expr) => {
346-
if let Some(
347-
Node::Local(hir::Local { span, .. })
348-
| Node::Item(hir::Item {
349-
kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
350-
span,
351-
..
352-
}),
353-
) = get_parent_node(cx.tcx, expr.hir_id)
354-
{
355-
let hir_id = match get_parent_node(cx.tcx, expr.hir_id) {
356-
Some(Node::Local(hir::Local { hir_id, .. })) => *hir_id,
357-
Some(Node::Item(hir::Item { owner_id, .. })) => {
358-
cx.tcx.hir().local_def_id_to_hir_id(owner_id.def_id)
359-
},
360-
_ => unreachable!(),
361-
};
362-
363-
// if unsafe block is part of a let/const/static statement,
364-
// and accept_comment_above_statement is set to true
365-
// we accept the safety comment in the line the precedes this statement.
366-
accept_comment_above_statement
367-
&& span_with_attrs_in_body_has_safety_comment(
368-
cx,
369-
*span,
370-
hir_id,
371-
accept_comment_above_attributes,
372-
)
373-
} else {
374-
!is_branchy(expr)
375-
&& span_with_attrs_in_body_has_safety_comment(
376-
cx,
377-
expr.span,
378-
expr.hir_id,
379-
accept_comment_above_attributes,
380-
)
381-
}
344+
let (span, hir_id) = match node {
345+
Node::Expr(expr) => match get_parent_node(cx.tcx, expr.hir_id) {
346+
Some(Node::Local(hir::Local { span, hir_id, .. })) => (*span, *hir_id),
347+
Some(Node::Item(hir::Item {
348+
kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
349+
span,
350+
owner_id,
351+
..
352+
})) => (*span, cx.tcx.hir().local_def_id_to_hir_id(owner_id.def_id)),
353+
_ => {
354+
if is_branchy(expr) {
355+
return false;
356+
}
357+
(expr.span, expr.hir_id)
358+
},
382359
},
383360
Node::Stmt(hir::Stmt {
384361
kind:
@@ -387,28 +364,27 @@ fn block_parents_have_safety_comment(
387364
| hir::StmtKind::Semi(hir::Expr { span, hir_id, .. }),
388365
..
389366
})
390-
| Node::Local(hir::Local { span, hir_id, .. }) => {
391-
span_with_attrs_in_body_has_safety_comment(cx, *span, *hir_id, accept_comment_above_attributes)
392-
},
367+
| Node::Local(hir::Local { span, hir_id, .. }) => (*span, *hir_id),
393368
Node::Item(hir::Item {
394369
kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
395370
span,
396371
owner_id,
397372
..
398-
}) => span_with_attrs_in_body_has_safety_comment(
399-
cx,
400-
*span,
401-
cx.tcx.hir().local_def_id_to_hir_id(owner_id.def_id),
402-
accept_comment_above_attributes,
403-
),
404-
_ => false,
373+
}) => (*span, cx.tcx.hir().local_def_id_to_hir_id(owner_id.def_id)),
374+
_ => return false,
405375
};
376+
// if unsafe block is part of a let/const/static statement,
377+
// and accept_comment_above_statement is set to true
378+
// we accept the safety comment in the line the precedes this statement.
379+
accept_comment_above_statement
380+
&& span_with_attrs_has_safety_comment(cx, span, hir_id, accept_comment_above_attributes)
381+
} else {
382+
false
406383
}
407-
false
408384
}
409385

410386
/// Extends `span` to also include its attributes, then checks if that span has a safety comment.
411-
fn span_with_attrs_in_body_has_safety_comment(
387+
fn span_with_attrs_has_safety_comment(
412388
cx: &LateContext<'_>,
413389
span: Span,
414390
hir_id: HirId,
@@ -420,7 +396,7 @@ fn span_with_attrs_in_body_has_safety_comment(
420396
span
421397
};
422398

423-
span_in_body_has_safety_comment(cx, span)
399+
span_has_safety_comment(cx, span)
424400
}
425401

426402
/// Checks if an expression is "branchy", e.g. loop, match/if/etc.
@@ -444,7 +420,7 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
444420
matches!(
445421
span_from_macro_expansion_has_safety_comment(cx, span),
446422
HasSafetyComment::Yes(_)
447-
) || span_in_body_has_safety_comment(cx, span)
423+
) || span_has_safety_comment(cx, span)
448424
}
449425

450426
fn include_attrs_in_span(cx: &LateContext<'_>, hir_id: HirId, span: Span) -> Span {
@@ -639,29 +615,36 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
639615
let body = cx.enclosing_body?;
640616
let map = cx.tcx.hir();
641617
let mut span = map.body(body).value.span;
618+
let mut maybe_global_var = false;
642619
for (_, node) in map.parent_iter(body.hir_id) {
643620
match node {
644621
Node::Expr(e) => span = e.span,
645-
Node::Block(_)
646-
| Node::Arm(_)
647-
| Node::Stmt(_)
648-
| Node::Local(_)
649-
| Node::Item(hir::Item {
622+
Node::Block(_) | Node::Arm(_) | Node::Stmt(_) | Node::Local(_) => (),
623+
Node::Item(hir::Item {
650624
kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
651625
..
652-
}) => (),
626+
}) => maybe_global_var = true,
627+
Node::Item(hir::Item {
628+
kind: hir::ItemKind::Mod(_),
629+
span: item_span,
630+
..
631+
}) => {
632+
span = *item_span;
633+
break;
634+
},
635+
Node::Crate(mod_) if maybe_global_var => {
636+
span = mod_.spans.inner_span;
637+
},
653638
_ => break,
654639
}
655640
}
656641
Some(span)
657642
}
658643

659-
fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
644+
fn span_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
660645
let source_map = cx.sess().source_map();
661646
let ctxt = span.ctxt();
662-
if ctxt == SyntaxContext::root()
663-
&& let Some(search_span) = get_body_search_span(cx)
664-
{
647+
if ctxt.is_root() && let Some(search_span) = get_body_search_span(cx) {
665648
if let Ok(unsafe_line) = source_map.lookup_line(span.lo())
666649
&& let Some(body_span) = walk_span_to_context(search_span, SyntaxContext::root())
667650
&& let Ok(body_line) = source_map.lookup_line(body_span.lo())

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,18 @@ fn issue_8679<T: Copy>() {
564564
unsafe {}
565565
}
566566

567+
mod issue_11246 {
568+
// Safety: foo
569+
const _: () = unsafe {};
570+
571+
// Safety: A safety comment
572+
const FOO: () = unsafe {};
573+
574+
// Safety: bar
575+
static BAR: u8 = unsafe { 0 };
576+
}
577+
578+
// Safety: Another safety comment
579+
const FOO: () = unsafe {};
580+
567581
fn main() {}

0 commit comments

Comments
 (0)