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.
This commit is contained in:
Erin Petra Sofiya Moon 2022-02-14 14:21:43 -05:00
parent 52dd59ed21
commit e59cda9ee1
No known key found for this signature in database
GPG key ID: 0C8E8FC540FFF97D
4 changed files with 38 additions and 0 deletions

View file

@ -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>",