@@ -505,7 +505,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
505
505
} ,
506
506
Node :: Stmt ( stmt) => {
507
507
if let Node :: Block ( block) = cx. tcx . parent_hir_node ( stmt. hir_id ) {
508
- walk_span_to_context ( block. span , SyntaxContext :: root ( ) ) . map ( Span :: lo )
508
+ walk_span_to_context ( block. span , SyntaxContext :: root ( ) ) . map ( |sp| ( sp . lo ( ) , false ) )
509
509
} else {
510
510
// Problem getting the parent node. Pretend a comment was found.
511
511
return HasSafetyComment :: Maybe ;
@@ -518,18 +518,20 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
518
518
} ;
519
519
520
520
let source_map = cx. sess ( ) . source_map ( ) ;
521
- if let Some ( comment_start) = comment_start
521
+ // If the comment is in the first line of the file, there is no preceding line
522
+ if let Some ( ( comment_start, include_first_line_of_file) ) = comment_start
522
523
&& let Ok ( unsafe_line) = source_map. lookup_line ( item. span . lo ( ) )
523
524
&& let Ok ( comment_start_line) = source_map. lookup_line ( comment_start)
524
- && Arc :: ptr_eq ( & unsafe_line. sf , & comment_start_line. sf )
525
+ && ( include_first_line_of_file || Arc :: ptr_eq ( & unsafe_line. sf , & comment_start_line. sf ) )
525
526
&& let Some ( src) = unsafe_line. sf . src . as_deref ( )
526
527
{
527
528
return if comment_start_line. line >= unsafe_line. line {
528
529
HasSafetyComment :: No
529
530
} else {
530
531
match text_has_safety_comment (
531
532
src,
532
- & unsafe_line. sf . lines ( ) [ comment_start_line. line + 1 ..=unsafe_line. line ] ,
533
+ & unsafe_line. sf . lines ( )
534
+ [ ( comment_start_line. line + usize:: from ( !include_first_line_of_file) ) ..=unsafe_line. line ] ,
533
535
unsafe_line. sf . start_pos ,
534
536
) {
535
537
Some ( b) => HasSafetyComment :: Yes ( b) ,
@@ -597,23 +599,23 @@ fn comment_start_before_item_in_mod(
597
599
parent_mod : & hir:: Mod < ' _ > ,
598
600
parent_mod_span : Span ,
599
601
item : & hir:: Item < ' _ > ,
600
- ) -> Option < BytePos > {
602
+ ) -> Option < ( BytePos , bool ) > {
601
603
parent_mod. item_ids . iter ( ) . enumerate ( ) . find_map ( |( idx, item_id) | {
602
604
if * item_id == item. item_id ( ) {
603
605
if idx == 0 {
604
606
// mod A { /* comment */ unsafe impl T {} ... }
605
607
// ^------------------------------------------^ returns the start of this span
606
608
// ^---------------------^ finally checks comments in this range
607
609
if let Some ( sp) = walk_span_to_context ( parent_mod_span, SyntaxContext :: root ( ) ) {
608
- return Some ( sp. lo ( ) ) ;
610
+ return Some ( ( sp. lo ( ) , false ) ) ;
609
611
}
610
612
} else {
611
613
// some_item /* comment */ unsafe impl T {}
612
614
// ^-------^ returns the end of this span
613
615
// ^---------------^ finally checks comments in this range
614
616
let prev_item = cx. tcx . hir_item ( parent_mod. item_ids [ idx - 1 ] ) ;
615
617
if let Some ( sp) = walk_span_to_context ( prev_item. span , SyntaxContext :: root ( ) ) {
616
- return Some ( sp. hi ( ) ) ;
618
+ return Some ( ( sp. hi ( ) , sp . is_dummy ( ) ) ) ;
617
619
}
618
620
}
619
621
}
@@ -668,7 +670,7 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
668
670
} ) => {
669
671
return maybe_mod_item
670
672
. and_then ( |item| comment_start_before_item_in_mod ( cx, mod_, * span, & item) )
671
- . map ( |comment_start| mod_. spans . inner_span . with_lo ( comment_start) )
673
+ . map ( |( comment_start, _ ) | mod_. spans . inner_span . with_lo ( comment_start) )
672
674
. or ( Some ( * span) ) ;
673
675
} ,
674
676
node if let Some ( ( span, _) ) = span_and_hid_of_item_alike_node ( & node)
0 commit comments