Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest using raw strings when invalid escapes appear in literals #93999

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ pub(crate) fn emit_unescape_error(
version control settings",
);
} else {
if !mode.is_bytes() {
diag.span_suggestion(
span_with_quotes,
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal",
format!("r\"{}\"", lit),
Applicability::MaybeIncorrect,
);
}

diag.help(
"for more information, visit \
<https://static.rust-lang.org/doc/master/reference.html#literals>",
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/lexer/lex-bad-char-literals-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ LL | '\●'
| ^ unknown character escape
|
= help: for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL | r"\●"
| ~~~~~

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

error: aborting due to 4 previous errors

7 changes: 7 additions & 0 deletions src/test/ui/parser/bad-escape-suggest-raw-string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let ok = r"ab\[c";
let bad = "ab\[c";
//~^ ERROR unknown character escape: `[`
//~| HELP for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
//~| HELP if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
}
14 changes: 14 additions & 0 deletions src/test/ui/parser/bad-escape-suggest-raw-string.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unknown character escape: `[`
--> $DIR/bad-escape-suggest-raw-string.rs:3:19
|
LL | let bad = "ab\[c";
| ^ unknown character escape
|
= help: for more information, visit <https://static.rust-lang.org/doc/master/reference.html#literals>
help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal
|
LL | let bad = r"ab\[c";
| ~~~~~~~~

error: aborting due to previous error