@@ -341,44 +341,21 @@ fn block_parents_have_safety_comment(
341
341
id: hir::HirId,
342
342
) -> bool {
343
343
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
+ },
382
359
},
383
360
Node::Stmt(hir::Stmt {
384
361
kind:
@@ -387,28 +364,27 @@ fn block_parents_have_safety_comment(
387
364
| hir::StmtKind::Semi(hir::Expr { span, hir_id, .. }),
388
365
..
389
366
})
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),
393
368
Node::Item(hir::Item {
394
369
kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
395
370
span,
396
371
owner_id,
397
372
..
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,
405
375
};
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
406
383
}
407
- false
408
384
}
409
385
410
386
/// 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 (
412
388
cx: &LateContext<'_>,
413
389
span: Span,
414
390
hir_id: HirId,
@@ -420,7 +396,7 @@ fn span_with_attrs_in_body_has_safety_comment(
420
396
span
421
397
};
422
398
423
- span_in_body_has_safety_comment (cx, span)
399
+ span_has_safety_comment (cx, span)
424
400
}
425
401
426
402
/// 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 {
444
420
matches!(
445
421
span_from_macro_expansion_has_safety_comment(cx, span),
446
422
HasSafetyComment::Yes(_)
447
- ) || span_in_body_has_safety_comment (cx, span)
423
+ ) || span_has_safety_comment (cx, span)
448
424
}
449
425
450
426
fn include_attrs_in_span(cx: &LateContext<'_>, hir_id: HirId, span: Span) -> Span {
@@ -639,29 +615,42 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
639
615
let body = cx.enclosing_body?;
640
616
let map = cx.tcx.hir();
641
617
let mut span = map.body(body).value.span;
618
+ let mut maybe_global_var = false;
642
619
for (_, node) in map.parent_iter(body.hir_id) {
643
620
match node {
644
- Node::Expr(e) => span = e.span,
621
+ Node::Expr(e) => {
622
+ span = e.span;
623
+ // Note: setting this to `false` is to making sure a "in-function defined"
624
+ // const/static variable not mistakenly processed as global variable,
625
+ // since global var doesn't have an `Expr` parent as its parent???
626
+ maybe_global_var = false;
627
+ }
645
628
Node::Block(_)
646
629
| Node::Arm(_)
647
630
| Node::Stmt(_)
648
- | Node::Local(_)
649
- | Node::Item(hir::Item {
650
- kind: hir::ItemKind::Const(..) | ItemKind::Static(..),
651
- ..
652
- }) => (),
631
+ | Node::Local(_) => (),
632
+ Node::Item(hir::Item { kind, span: item_span, .. }) => {
633
+ if matches!(kind, hir::ItemKind::Const(..) | ItemKind::Static(..)) {
634
+ maybe_global_var = true;
635
+ } else if maybe_global_var && let hir::ItemKind::Mod(_) = kind {
636
+ span = *item_span;
637
+ } else {
638
+ break;
639
+ }
640
+ }
641
+ Node::Crate(mod_) if maybe_global_var => {
642
+ span = mod_.spans.inner_span;
643
+ }
653
644
_ => break,
654
645
}
655
646
}
656
647
Some(span)
657
648
}
658
649
659
- fn span_in_body_has_safety_comment (cx: &LateContext<'_>, span: Span) -> bool {
650
+ fn span_has_safety_comment (cx: &LateContext<'_>, span: Span) -> bool {
660
651
let source_map = cx.sess().source_map();
661
652
let ctxt = span.ctxt();
662
- if ctxt == SyntaxContext::root()
663
- && let Some(search_span) = get_body_search_span(cx)
664
- {
653
+ if ctxt.is_root() && let Some(search_span) = get_body_search_span(cx) {
665
654
if let Ok(unsafe_line) = source_map.lookup_line(span.lo())
666
655
&& let Some(body_span) = walk_span_to_context(search_span, SyntaxContext::root())
667
656
&& let Ok(body_line) = source_map.lookup_line(body_span.lo())
0 commit comments