@@ -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
450426fn 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> {
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 {
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+ }
645628 Node :: Block ( _)
646629 | Node :: Arm ( _)
647630 | 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+ }
653644 _ => break ,
654645 }
655646 }
656647 Some ( span)
657648}
658649
659- fn span_in_body_has_safety_comment ( cx : & LateContext < ' _ > , span : Span ) -> bool {
650+ fn span_has_safety_comment ( cx : & LateContext < ' _ > , span : Span ) -> bool {
660651 let source_map = cx. sess ( ) . source_map ( ) ;
661652 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) {
665654 if let Ok ( unsafe_line) = source_map. lookup_line ( span. lo ( ) )
666655 && let Some ( body_span) = walk_span_to_context ( search_span, SyntaxContext :: root ( ) )
667656 && let Ok ( body_line) = source_map. lookup_line ( body_span. lo ( ) )
0 commit comments