@@ -343,12 +343,13 @@ fn block_parents_have_safety_comment(
343
343
let ( span, hir_id) = match cx. tcx . parent_hir_node ( id) {
344
344
Node :: Expr ( expr) => match cx. tcx . parent_hir_node ( expr. hir_id ) {
345
345
Node :: LetStmt ( hir:: LetStmt { span, hir_id, .. } ) => ( * span, * hir_id) ,
346
- Node :: Item ( hir:: Item {
347
- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
348
- span,
349
- owner_id,
350
- ..
351
- } ) => ( * span, cx. tcx . local_def_id_to_hir_id ( owner_id. def_id ) ) ,
346
+
347
+ node if let Some ( item) = ItemAlike :: hir ( & node)
348
+ && item. is_const_or_static ( ) =>
349
+ {
350
+ ( item. span , cx. tcx . local_def_id_to_hir_id ( item. owner_id . def_id ) )
351
+ } ,
352
+
352
353
_ => {
353
354
if is_branchy ( expr) {
354
355
return false ;
@@ -364,12 +365,13 @@ fn block_parents_have_safety_comment(
364
365
..
365
366
} )
366
367
| Node :: LetStmt ( hir:: LetStmt { span, hir_id, .. } ) => ( * span, * hir_id) ,
367
- Node :: Item ( hir:: Item {
368
- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
369
- span,
370
- owner_id,
371
- ..
372
- } ) => ( * span, cx. tcx . local_def_id_to_hir_id ( owner_id. def_id ) ) ,
368
+
369
+ node if let Some ( item) = ItemAlike :: hir ( & node)
370
+ && item. is_const_or_static ( ) =>
371
+ {
372
+ ( item. span , cx. tcx . local_def_id_to_hir_id ( item. owner_id . def_id ) )
373
+ } ,
374
+
373
375
_ => return false ,
374
376
} ;
375
377
// if unsafe block is part of a let/const/static statement,
@@ -597,32 +599,18 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
597
599
598
600
fn get_body_search_span ( cx : & LateContext < ' _ > ) -> Option < Span > {
599
601
let body = cx. enclosing_body ?;
600
- let map = cx. tcx . hir ( ) ;
601
- let mut span = map. body ( body) . value . span ;
602
- let mut maybe_global_var = false ;
603
- for ( _, node) in map. parent_iter ( body. hir_id ) {
604
- match node {
605
- Node :: Expr ( e) => span = e. span ,
606
- Node :: Block ( _) | Node :: Arm ( _) | Node :: Stmt ( _) | Node :: LetStmt ( _) => ( ) ,
607
- Node :: Item ( hir:: Item {
608
- kind : ItemKind :: Const ( ..) | ItemKind :: Static ( ..) ,
609
- ..
610
- } ) => maybe_global_var = true ,
611
- Node :: Item ( hir:: Item {
612
- kind : ItemKind :: Mod ( _) ,
613
- span : item_span,
614
- ..
615
- } ) => {
616
- span = * item_span;
617
- break ;
602
+ for ( _, parent_node) in cx. tcx . hir ( ) . parent_iter ( body. hir_id ) {
603
+ match parent_node {
604
+ Node :: Crate ( mod_) => return Some ( mod_. spans . inner_span ) ,
605
+ node if let Some ( item) = ItemAlike :: hir ( & node)
606
+ && !item. is_const_or_static ( ) =>
607
+ {
608
+ return Some ( item. span ) ;
618
609
} ,
619
- Node :: Crate ( mod_) if maybe_global_var => {
620
- span = mod_. spans . inner_span ;
621
- } ,
622
- _ => break ,
610
+ _ => { } ,
623
611
}
624
612
}
625
- Some ( span )
613
+ None
626
614
}
627
615
628
616
fn span_has_safety_comment ( cx : & LateContext < ' _ > , span : Span ) -> bool {
@@ -711,3 +699,53 @@ fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos
711
699
}
712
700
}
713
701
}
702
+
703
+ macro_rules! item_like_kind_conversion {
704
+ ( $varient: ident, $ty_convert_from: ty, $lft: lifetime) => {
705
+ impl <$lft> :: core:: convert:: From <$ty_convert_from> for ItemAlikeKind <$lft> {
706
+ fn from( value: $ty_convert_from) -> Self {
707
+ Self :: $varient( value)
708
+ }
709
+ }
710
+ } ;
711
+ }
712
+
713
+ enum ItemAlikeKind < ' hir > {
714
+ Item ( ItemKind < ' hir > ) ,
715
+ TraitItem ( hir:: TraitItemKind < ' hir > ) ,
716
+ ImplItem ( hir:: ImplItemKind < ' hir > ) ,
717
+ }
718
+
719
+ item_like_kind_conversion ! ( Item , ItemKind <' hir>, ' hir) ;
720
+ item_like_kind_conversion ! ( TraitItem , hir:: TraitItemKind <' hir>, ' hir) ;
721
+ item_like_kind_conversion ! ( ImplItem , hir:: ImplItemKind <' hir>, ' hir) ;
722
+
723
+ /// Representing the hir nodes that are item alike.
724
+ ///
725
+ /// Note this does not includes [`Node::ForeignItem`] for now.
726
+ struct ItemAlike < ' hir > {
727
+ owner_id : hir:: OwnerId ,
728
+ kind : ItemAlikeKind < ' hir > ,
729
+ span : Span ,
730
+ }
731
+
732
+ impl < ' hir > ItemAlike < ' hir > {
733
+ fn hir ( node : & ' hir Node < ' hir > ) -> Option < Self > {
734
+ let ( owner_id, kind, span) = match node {
735
+ Node :: Item ( item) => ( item. owner_id , item. kind . into ( ) , item. span ) ,
736
+ Node :: TraitItem ( ti) => ( ti. owner_id , ti. kind . into ( ) , ti. span ) ,
737
+ Node :: ImplItem ( ii) => ( ii. owner_id , ii. kind . into ( ) , ii. span ) ,
738
+ _ => return None ,
739
+ } ;
740
+ Some ( Self { owner_id, kind, span } )
741
+ }
742
+
743
+ fn is_const_or_static ( & self ) -> bool {
744
+ matches ! (
745
+ self . kind,
746
+ ItemAlikeKind :: Item ( ItemKind :: Const ( ..) | ItemKind :: Static ( ..) )
747
+ | ItemAlikeKind :: ImplItem ( hir:: ImplItemKind :: Const ( ..) )
748
+ | ItemAlikeKind :: TraitItem ( hir:: TraitItemKind :: Const ( ..) )
749
+ )
750
+ }
751
+ }
0 commit comments