about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorErin Petra Sofiya Moon <emoon@starry.com>2022-02-14 14:21:43 -0500
committerErin Petra Sofiya Moon <emoon@starry.com>2022-02-14 15:11:38 -0500
commite59cda9ee1b9fab3fe966ea7e4b12c14b1f85789 (patch)
treea731617118a0361d343c6e7d9c48f84416cc3f33 /src/test
parent52dd59ed2154f4158ae37e6994b678a6249a7bb0 (diff)
downloadrust-e59cda9ee1b9fab3fe966ea7e4b12c14b1f85789.tar.gz
rust-e59cda9ee1b9fab3fe966ea7e4b12c14b1f85789.zip
suggest using raw string literals when invalid escapes appear
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.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/lexer/lex-bad-char-literals-1.stderr8
-rw-r--r--src/test/ui/parser/bad-escape-suggest-raw-string.rs7
-rw-r--r--src/test/ui/parser/bad-escape-suggest-raw-string.stderr14
3 files changed, 29 insertions, 0 deletions
diff --git a/src/test/ui/lexer/lex-bad-char-literals-1.stderr b/src/test/ui/lexer/lex-bad-char-literals-1.stderr
index ed129a1d133..e6ff1f662bd 100644
--- a/src/test/ui/lexer/lex-bad-char-literals-1.stderr
+++ b/src/test/ui/lexer/lex-bad-char-literals-1.stderr
@@ -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
@@ -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
 
diff --git a/src/test/ui/parser/bad-escape-suggest-raw-string.rs b/src/test/ui/parser/bad-escape-suggest-raw-string.rs
new file mode 100644
index 00000000000..978b92cbcd2
--- /dev/null
+++ b/src/test/ui/parser/bad-escape-suggest-raw-string.rs
@@ -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
+}
diff --git a/src/test/ui/parser/bad-escape-suggest-raw-string.stderr b/src/test/ui/parser/bad-escape-suggest-raw-string.stderr
new file mode 100644
index 00000000000..fc34bd3281a
--- /dev/null
+++ b/src/test/ui/parser/bad-escape-suggest-raw-string.stderr
@@ -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
+