@@ -12,7 +12,7 @@ use rustc_lexer::{tokenize, TokenKind};
1212use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1313use rustc_middle:: lint:: in_external_macro;
1414use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
15- use rustc_span:: { BytePos , Pos , Span , SyntaxContext } ;
15+ use rustc_span:: { BytePos , Pos , RelativeBytePos , Span , SyntaxContext } ;
1616
1717declare_clippy_lint ! {
1818 /// ### What it does
@@ -514,7 +514,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
514514 match text_has_safety_comment (
515515 src,
516516 & lines[ comment_start_line. line + 1 ..=unsafe_line. line ] ,
517- unsafe_line. sf . start_pos . to_usize ( ) ,
517+ unsafe_line. sf . start_pos ,
518518 ) {
519519 Some ( b) => HasSafetyComment :: Yes ( b) ,
520520 None => HasSafetyComment :: No ,
@@ -558,7 +558,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
558558 match text_has_safety_comment (
559559 src,
560560 & lines[ comment_start_line. line + 1 ..=unsafe_line. line ] ,
561- unsafe_line. sf . start_pos . to_usize ( ) ,
561+ unsafe_line. sf . start_pos ,
562562 ) {
563563 Some ( b) => HasSafetyComment :: Yes ( b) ,
564564 None => HasSafetyComment :: No ,
@@ -619,7 +619,7 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
619619 match text_has_safety_comment (
620620 src,
621621 & lines[ macro_line. line + 1 ..=unsafe_line. line ] ,
622- unsafe_line. sf . start_pos . to_usize ( ) ,
622+ unsafe_line. sf . start_pos ,
623623 ) {
624624 Some ( b) => HasSafetyComment :: Yes ( b) ,
625625 None => HasSafetyComment :: No ,
@@ -675,7 +675,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
675675 body_line. line < unsafe_line. line && text_has_safety_comment (
676676 src,
677677 & lines[ body_line. line + 1 ..=unsafe_line. line ] ,
678- unsafe_line. sf . start_pos . to_usize ( ) ,
678+ unsafe_line. sf . start_pos ,
679679 ) . is_some ( )
680680 } )
681681 } else {
@@ -688,13 +688,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
688688}
689689
690690/// Checks if the given text has a safety comment for the immediately proceeding line.
691- fn text_has_safety_comment ( src : & str , line_starts : & [ BytePos ] , offset : usize ) -> Option < BytePos > {
691+ fn text_has_safety_comment ( src : & str , line_starts : & [ RelativeBytePos ] , start_pos : BytePos ) -> Option < BytePos > {
692692 let mut lines = line_starts
693693 . array_windows :: < 2 > ( )
694694 . rev ( )
695695 . map_while ( |[ start, end] | {
696- let start = start. to_usize ( ) - offset ;
697- let end = end. to_usize ( ) - offset ;
696+ let start = start. to_usize ( ) ;
697+ let end = end. to_usize ( ) ;
698698 let text = src. get ( start..end) ?;
699699 let trimmed = text. trim_start ( ) ;
700700 Some ( ( start + ( text. len ( ) - trimmed. len ( ) ) , trimmed) )
@@ -709,9 +709,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
709709 let ( mut line, mut line_start) = ( line, line_start) ;
710710 loop {
711711 if line. to_ascii_uppercase ( ) . contains ( "SAFETY:" ) {
712- return Some ( BytePos (
713- u32:: try_from ( line_start) . unwrap ( ) + u32:: try_from ( offset) . unwrap ( ) ,
714- ) ) ;
712+ return Some ( start_pos + BytePos ( u32:: try_from ( line_start) . unwrap ( ) ) ) ;
715713 }
716714 match lines. next ( ) {
717715 Some ( ( s, x) ) if x. starts_with ( "//" ) => ( line, line_start) = ( x, s) ,
@@ -724,15 +722,13 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
724722 let ( mut line_start, mut line) = ( line_start, line) ;
725723 loop {
726724 if line. starts_with ( "/*" ) {
727- let src = & src[ line_start..line_starts. last ( ) . unwrap ( ) . to_usize ( ) - offset ] ;
725+ let src = & src[ line_start..line_starts. last ( ) . unwrap ( ) . to_usize ( ) ] ;
728726 let mut tokens = tokenize ( src) ;
729727 return ( src[ ..tokens. next ( ) . unwrap ( ) . len as usize ]
730728 . to_ascii_uppercase ( )
731729 . contains ( "SAFETY:" )
732730 && tokens. all ( |t| t. kind == TokenKind :: Whitespace ) )
733- . then_some ( BytePos (
734- u32:: try_from ( line_start) . unwrap ( ) + u32:: try_from ( offset) . unwrap ( ) ,
735- ) ) ;
731+ . then_some ( start_pos + BytePos ( u32:: try_from ( line_start) . unwrap ( ) ) ) ;
736732 }
737733 match lines. next ( ) {
738734 Some ( x) => ( line_start, line) = x,
0 commit comments