@@ -2,7 +2,7 @@ use std::iter::once;
22use std:: ops:: ControlFlow ;
33
44use clippy_utils:: diagnostics:: span_lint_and_then;
5- use clippy_utils:: source:: snippet ;
5+ use clippy_utils:: source:: SpanRangeExt ;
66use rustc_ast:: ast:: { Expr , ExprKind } ;
77use rustc_ast:: token:: LitKind ;
88use rustc_errors:: Applicability ;
@@ -63,20 +63,17 @@ pub struct RawStrings {
6363
6464impl EarlyLintPass for RawStrings {
6565 fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & Expr ) {
66- if !in_external_macro ( cx. sess ( ) , expr. span )
67- && let ExprKind :: Lit ( lit) = expr. kind
68- && let LitKind :: StrRaw ( max) | LitKind :: ByteStrRaw ( max) | LitKind :: CStrRaw ( max) = lit. kind
66+ if let ExprKind :: Lit ( lit) = expr. kind
67+ && let ( prefix, max) = match lit. kind {
68+ LitKind :: StrRaw ( max) => ( "r" , max) ,
69+ LitKind :: ByteStrRaw ( max) => ( "br" , max) ,
70+ LitKind :: CStrRaw ( max) => ( "cr" , max) ,
71+ _ => return ,
72+ }
73+ && !in_external_macro ( cx. sess ( ) , expr. span )
74+ && expr. span . check_source_text ( cx, |src| src. starts_with ( prefix) )
6975 {
7076 let str = lit. symbol . as_str ( ) ;
71- let prefix = match lit. kind {
72- LitKind :: StrRaw ( ..) => "r" ,
73- LitKind :: ByteStrRaw ( ..) => "br" ,
74- LitKind :: CStrRaw ( ..) => "cr" ,
75- _ => unreachable ! ( ) ,
76- } ;
77- if !snippet ( cx, expr. span , prefix) . trim ( ) . starts_with ( prefix) {
78- return ;
79- }
8077 let descr = lit. kind . descr ( ) ;
8178
8279 if !str. contains ( [ '\\' , '"' ] ) {
@@ -86,7 +83,7 @@ impl EarlyLintPass for RawStrings {
8683 expr. span ,
8784 "unnecessary raw string literal" ,
8885 |diag| {
89- let ( start, end) = hash_spans ( expr. span , prefix, 0 , max) ;
86+ let ( start, end) = hash_spans ( expr. span , prefix. len ( ) , 0 , max) ;
9087
9188 // BytePos: skip over the `b` in `br`, we checked the prefix appears in the source text
9289 let r_pos = expr. span . lo ( ) + BytePos :: from_usize ( prefix. len ( ) - 1 ) ;
@@ -148,7 +145,7 @@ impl EarlyLintPass for RawStrings {
148145 expr. span ,
149146 "unnecessary hashes around raw string literal" ,
150147 |diag| {
151- let ( start, end) = hash_spans ( expr. span , prefix, req, max) ;
148+ let ( start, end) = hash_spans ( expr. span , prefix. len ( ) , req, max) ;
152149
153150 let message = match max - req {
154151 _ if req == 0 => format ! ( "remove all the hashes around the {descr} literal" ) ,
@@ -174,11 +171,11 @@ impl EarlyLintPass for RawStrings {
174171/// r###".."###
175172/// ^^ ^^
176173/// ```
177- fn hash_spans ( literal_span : Span , prefix : & str , req : u8 , max : u8 ) -> ( Span , Span ) {
174+ fn hash_spans ( literal_span : Span , prefix_len : usize , req : u8 , max : u8 ) -> ( Span , Span ) {
178175 let literal_span = literal_span. data ( ) ;
179176
180177 // BytePos: we checked prefix appears literally in the source text
181- let hash_start = literal_span. lo + BytePos :: from_usize ( prefix . len ( ) ) ;
178+ let hash_start = literal_span. lo + BytePos :: from_usize ( prefix_len ) ;
182179 let hash_end = literal_span. hi ;
183180
184181 // BytePos: req/max are counts of the ASCII character #
0 commit comments