@@ -2,7 +2,7 @@ use std::iter::once;
2
2
use std:: ops:: ControlFlow ;
3
3
4
4
use clippy_utils:: diagnostics:: span_lint_and_then;
5
- use clippy_utils:: source:: snippet ;
5
+ use clippy_utils:: source:: SpanRangeExt ;
6
6
use rustc_ast:: ast:: { Expr , ExprKind } ;
7
7
use rustc_ast:: token:: LitKind ;
8
8
use rustc_errors:: Applicability ;
@@ -63,20 +63,17 @@ pub struct RawStrings {
63
63
64
64
impl EarlyLintPass for RawStrings {
65
65
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) )
69
75
{
70
76
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
- }
80
77
let descr = lit. kind . descr ( ) ;
81
78
82
79
if !str. contains ( [ '\\' , '"' ] ) {
@@ -86,7 +83,7 @@ impl EarlyLintPass for RawStrings {
86
83
expr. span ,
87
84
"unnecessary raw string literal" ,
88
85
|diag| {
89
- let ( start, end) = hash_spans ( expr. span , prefix, 0 , max) ;
86
+ let ( start, end) = hash_spans ( expr. span , prefix. len ( ) , 0 , max) ;
90
87
91
88
// BytePos: skip over the `b` in `br`, we checked the prefix appears in the source text
92
89
let r_pos = expr. span . lo ( ) + BytePos :: from_usize ( prefix. len ( ) - 1 ) ;
@@ -148,7 +145,7 @@ impl EarlyLintPass for RawStrings {
148
145
expr. span ,
149
146
"unnecessary hashes around raw string literal" ,
150
147
|diag| {
151
- let ( start, end) = hash_spans ( expr. span , prefix, req, max) ;
148
+ let ( start, end) = hash_spans ( expr. span , prefix. len ( ) , req, max) ;
152
149
153
150
let message = match max - req {
154
151
_ if req == 0 => format ! ( "remove all the hashes around the {descr} literal" ) ,
@@ -174,11 +171,11 @@ impl EarlyLintPass for RawStrings {
174
171
/// r###".."###
175
172
/// ^^ ^^
176
173
/// ```
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 ) {
178
175
let literal_span = literal_span. data ( ) ;
179
176
180
177
// 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 ) ;
182
179
let hash_end = literal_span. hi ;
183
180
184
181
// BytePos: req/max are counts of the ASCII character #
0 commit comments