@@ -339,6 +339,33 @@ fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool {
339339 . is_none_or ( |src| !src. starts_with ( "unsafe" ) )
340340}
341341
342+ fn find_unsafe_block_parent_in_expr < ' tcx > (
343+ cx : & LateContext < ' tcx > ,
344+ expr : & ' tcx hir:: Expr < ' tcx > ,
345+ ) -> Option < ( Span , HirId ) > {
346+ match cx. tcx . parent_hir_node ( expr. hir_id ) {
347+ Node :: LetStmt ( hir:: LetStmt { span, hir_id, .. } )
348+ | Node :: Expr ( hir:: Expr {
349+ hir_id,
350+ kind : hir:: ExprKind :: Assign ( _, _, span) ,
351+ ..
352+ } ) => Some ( ( * span, * hir_id) ) ,
353+ Node :: Expr ( expr) => find_unsafe_block_parent_in_expr ( cx, expr) ,
354+ node if let Some ( ( span, hir_id) ) = span_and_hid_of_item_alike_node ( & node)
355+ && is_const_or_static ( & node) =>
356+ {
357+ Some ( ( span, hir_id) )
358+ } ,
359+
360+ _ => {
361+ if is_branchy ( expr) {
362+ return None ;
363+ }
364+ Some ( ( expr. span , expr. hir_id ) )
365+ } ,
366+ }
367+ }
368+
342369// Checks if any parent {expression, statement, block, local, const, static}
343370// has a safety comment
344371fn block_parents_have_safety_comment (
@@ -348,21 +375,7 @@ fn block_parents_have_safety_comment(
348375 id : HirId ,
349376) -> bool {
350377 let ( span, hir_id) = match cx. tcx . parent_hir_node ( id) {
351- Node :: Expr ( expr) => match cx. tcx . parent_hir_node ( expr. hir_id ) {
352- Node :: LetStmt ( hir:: LetStmt { span, hir_id, .. } ) => ( * span, * hir_id) ,
353- Node :: Item ( hir:: Item {
354- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
355- span,
356- owner_id,
357- ..
358- } ) => ( * span, cx. tcx . local_def_id_to_hir_id ( owner_id. def_id ) ) ,
359- _ => {
360- if is_branchy ( expr) {
361- return false ;
362- }
363- ( expr. span , expr. hir_id )
364- } ,
365- } ,
378+ Node :: Expr ( expr) if let Some ( inner) = find_unsafe_block_parent_in_expr ( cx, expr) => inner,
366379 Node :: Stmt ( hir:: Stmt {
367380 kind :
368381 hir:: StmtKind :: Let ( hir:: LetStmt { span, hir_id, .. } )
@@ -371,12 +384,13 @@ fn block_parents_have_safety_comment(
371384 ..
372385 } )
373386 | Node :: LetStmt ( hir:: LetStmt { span, hir_id, .. } ) => ( * span, * hir_id) ,
374- Node :: Item ( hir:: Item {
375- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
376- span,
377- owner_id,
378- ..
379- } ) => ( * span, cx. tcx . local_def_id_to_hir_id ( owner_id. def_id ) ) ,
387+
388+ node if let Some ( ( span, hir_id) ) = span_and_hid_of_item_alike_node ( & node)
389+ && is_const_or_static ( & node) =>
390+ {
391+ ( span, hir_id)
392+ } ,
393+
380394 _ => return false ,
381395 } ;
382396 // if unsafe block is part of a let/const/static statement,
@@ -427,12 +441,12 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
427441}
428442
429443fn include_attrs_in_span ( cx : & LateContext < ' _ > , hir_id : HirId , span : Span ) -> Span {
430- span. to ( cx
431- . tcx
432- . hir ( )
433- . attrs ( hir_id )
434- . iter ( )
435- . fold ( span , |acc , attr| acc . to ( attr . span ( ) ) ) )
444+ span. to ( cx. tcx . hir ( ) . attrs ( hir_id ) . iter ( ) . fold ( span , |acc , attr| {
445+ if attr . is_doc_comment ( ) {
446+ return acc ;
447+ }
448+ acc . to ( attr . span ( ) )
449+ } ) )
436450}
437451
438452enum HasSafetyComment {
@@ -604,31 +618,35 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
604618
605619fn get_body_search_span ( cx : & LateContext < ' _ > ) -> Option < Span > {
606620 let body = cx. enclosing_body ?;
607- let mut span = cx. tcx . hir_body ( body) . value . span ;
608- let mut maybe_global_var = false ;
609- for ( _, node) in cx. tcx . hir_parent_iter ( body. hir_id ) {
610- match node {
611- Node :: Expr ( e) => span = e. span ,
612- Node :: Block ( _) | Node :: Arm ( _) | Node :: Stmt ( _) | Node :: LetStmt ( _) => ( ) ,
613- Node :: Item ( hir:: Item {
614- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
615- ..
616- } ) => maybe_global_var = true ,
621+ let mut maybe_mod_item = None ;
622+
623+ for ( _, parent_node) in cx. tcx . hir_parent_iter ( body. hir_id ) {
624+ match parent_node {
625+ Node :: Crate ( mod_) => return Some ( mod_. spans . inner_span ) ,
617626 Node :: Item ( hir:: Item {
618- kind : ItemKind :: Mod ( _ ) ,
619- span : item_span ,
627+ kind : ItemKind :: Mod ( mod_ ) ,
628+ span,
620629 ..
621630 } ) => {
622- span = * item_span;
623- break ;
631+ return maybe_mod_item
632+ . and_then ( |item| comment_start_before_item_in_mod ( cx, mod_, * span, & item) )
633+ . map ( |comment_start| mod_. spans . inner_span . with_lo ( comment_start) )
634+ . or ( Some ( * span) ) ;
635+ } ,
636+ node if let Some ( ( span, _) ) = span_and_hid_of_item_alike_node ( & node)
637+ && !is_const_or_static ( & node) =>
638+ {
639+ return Some ( span) ;
640+ } ,
641+ Node :: Item ( item) => {
642+ maybe_mod_item = Some ( * item) ;
624643 } ,
625- Node :: Crate ( mod_ ) if maybe_global_var => {
626- span = mod_ . spans . inner_span ;
644+ _ => {
645+ maybe_mod_item = None ;
627646 } ,
628- _ => break ,
629647 }
630648 }
631- Some ( span )
649+ None
632650}
633651
634652fn span_has_safety_comment ( cx : & LateContext < ' _ > , span : Span ) -> bool {
@@ -717,3 +735,28 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos
717735 }
718736 }
719737}
738+
739+ fn span_and_hid_of_item_alike_node ( node : & Node < ' _ > ) -> Option < ( Span , HirId ) > {
740+ match node {
741+ Node :: Item ( item) => Some ( ( item. span , item. owner_id . into ( ) ) ) ,
742+ Node :: TraitItem ( ti) => Some ( ( ti. span , ti. owner_id . into ( ) ) ) ,
743+ Node :: ImplItem ( ii) => Some ( ( ii. span , ii. owner_id . into ( ) ) ) ,
744+ _ => None ,
745+ }
746+ }
747+
748+ fn is_const_or_static ( node : & Node < ' _ > ) -> bool {
749+ matches ! (
750+ node,
751+ Node :: Item ( hir:: Item {
752+ kind: ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
753+ ..
754+ } ) | Node :: ImplItem ( hir:: ImplItem {
755+ kind: hir:: ImplItemKind :: Const ( ..) ,
756+ ..
757+ } ) | Node :: TraitItem ( hir:: TraitItem {
758+ kind: hir:: TraitItemKind :: Const ( ..) ,
759+ ..
760+ } )
761+ )
762+ }
0 commit comments