Skip to content

Commit c6b27db

Browse files
authored
Rollup merge of #93999 - barzamin:suggest-raw-strings, r=jackh726
suggest using raw strings when invalid escapes appear in literals i'd guess about 70% of "bad escape" cases occur when someone meant to use a raw string literal because they're passing it directly to `Regex::new()`. this emits an advisory (`Applicability::MaybeIncorrect`) `help:` suggestion to the user that they use an `r""` string, on top of the normal notes about looking at the string literal documentation/spec.
2 parents e1baa3d + e59cda9 commit c6b27db

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ pub(crate) fn emit_unescape_error(
185185
version control settings",
186186
);
187187
} else {
188+
if !mode.is_bytes() {
189+
diag.span_suggestion(
190+
span_with_quotes,
191+
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal",
192+
format!("r\"{}\"", lit),
193+
Applicability::MaybeIncorrect,
194+
);
195+
}
196+
188197
diag.help(
189198
"for more information, visit \
190199
<https://static.rust-lang.org/doc/master/reference.html#literals>",

src/test/ui/lexer/lex-bad-char-literals-1.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ LL | '\●'
1717
| ^ unknown character escape
1818
|
1919
= help: for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
20+
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
21+
|
22+
LL | r"\●"
23+
| ~~~~~
2024

2125
error: unknown character escape: `\u{25cf}`
2226
--> $DIR/lex-bad-char-literals-1.rs:14:7
@@ -25,6 +29,10 @@ LL | "\●"
2529
| ^ unknown character escape
2630
|
2731
= help: for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
32+
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
33+
|
34+
LL | r"\●"
35+
| ~~~~~
2836

2937
error: aborting due to 4 previous errors
3038

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let ok = r"ab\[c";
3+
let bad = "ab\[c";
4+
//~^ ERROR unknown character escape: `[`
5+
//~| HELP for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
6+
//~| HELP if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unknown character escape: `[`
2+
--> $DIR/bad-escape-suggest-raw-string.rs:3:19
3+
|
4+
LL | let bad = "ab\[c";
5+
| ^ unknown character escape
6+
|
7+
= help: for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
8+
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
9+
|
10+
LL | let bad = r"ab\[c";
11+
| ~~~~~~~~
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)