@@ -505,7 +505,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
505505 } ,
506506 Node :: Stmt ( stmt) => {
507507 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 ) )
509509 } else {
510510 // Problem getting the parent node. Pretend a comment was found.
511511 return HasSafetyComment :: Maybe ;
@@ -518,18 +518,20 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
518518 } ;
519519
520520 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
522523 && let Ok ( unsafe_line) = source_map. lookup_line ( item. span . lo ( ) )
523524 && 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 ) )
525526 && let Some ( src) = unsafe_line. sf . src . as_deref ( )
526527 {
527528 return if comment_start_line. line >= unsafe_line. line {
528529 HasSafetyComment :: No
529530 } else {
530531 match text_has_safety_comment (
531532 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 ] ,
533535 unsafe_line. sf . start_pos ,
534536 ) {
535537 Some ( b) => HasSafetyComment :: Yes ( b) ,
@@ -597,23 +599,23 @@ fn comment_start_before_item_in_mod(
597599 parent_mod : & hir:: Mod < ' _ > ,
598600 parent_mod_span : Span ,
599601 item : & hir:: Item < ' _ > ,
600- ) -> Option < BytePos > {
602+ ) -> Option < ( BytePos , bool ) > {
601603 parent_mod. item_ids . iter ( ) . enumerate ( ) . find_map ( |( idx, item_id) | {
602604 if * item_id == item. item_id ( ) {
603605 if idx == 0 {
604606 // mod A { /* comment */ unsafe impl T {} ... }
605607 // ^------------------------------------------^ returns the start of this span
606608 // ^---------------------^ finally checks comments in this range
607609 if let Some ( sp) = walk_span_to_context ( parent_mod_span, SyntaxContext :: root ( ) ) {
608- return Some ( sp. lo ( ) ) ;
610+ return Some ( ( sp. lo ( ) , false ) ) ;
609611 }
610612 } else {
611613 // some_item /* comment */ unsafe impl T {}
612614 // ^-------^ returns the end of this span
613615 // ^---------------^ finally checks comments in this range
614616 let prev_item = cx. tcx . hir_item ( parent_mod. item_ids [ idx - 1 ] ) ;
615617 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 ( ) ) ) ;
617619 }
618620 }
619621 }
@@ -668,7 +670,7 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
668670 } ) => {
669671 return maybe_mod_item
670672 . 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) )
672674 . or ( Some ( * span) ) ;
673675 } ,
674676 node if let Some ( ( span, _) ) = span_and_hid_of_item_alike_node ( & node)
0 commit comments