@@ -339,6 +339,33 @@ fn is_unsafe_from_proc_macro(cx: &LateContext<'_>, span: Span) -> bool {
339
339
. is_none_or ( |src| !src. starts_with ( "unsafe" ) )
340
340
}
341
341
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
+
342
369
// Checks if any parent {expression, statement, block, local, const, static}
343
370
// has a safety comment
344
371
fn block_parents_have_safety_comment (
@@ -348,21 +375,7 @@ fn block_parents_have_safety_comment(
348
375
id : HirId ,
349
376
) -> bool {
350
377
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,
366
379
Node :: Stmt ( hir:: Stmt {
367
380
kind :
368
381
hir:: StmtKind :: Let ( hir:: LetStmt { span, hir_id, .. } )
@@ -371,12 +384,13 @@ fn block_parents_have_safety_comment(
371
384
..
372
385
} )
373
386
| 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
+
380
394
_ => return false ,
381
395
} ;
382
396
// 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 {
427
441
}
428
442
429
443
fn 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
+ } ) )
436
450
}
437
451
438
452
enum HasSafetyComment {
@@ -604,31 +618,35 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
604
618
605
619
fn get_body_search_span ( cx : & LateContext < ' _ > ) -> Option < Span > {
606
620
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 ) ,
617
626
Node :: Item ( hir:: Item {
618
- kind : ItemKind :: Mod ( _ ) ,
619
- span : item_span ,
627
+ kind : ItemKind :: Mod ( mod_ ) ,
628
+ span,
620
629
..
621
630
} ) => {
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) ;
624
643
} ,
625
- Node :: Crate ( mod_ ) if maybe_global_var => {
626
- span = mod_ . spans . inner_span ;
644
+ _ => {
645
+ maybe_mod_item = None ;
627
646
} ,
628
- _ => break ,
629
647
}
630
648
}
631
- Some ( span )
649
+ None
632
650
}
633
651
634
652
fn 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
717
735
}
718
736
}
719
737
}
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