From e59cda9ee1b9fab3fe966ea7e4b12c14b1f85789 Mon Sep 17 00:00:00 2001 From: Erin Petra Sofiya Moon Date: Mon, 14 Feb 2022 14:21:43 -0500 Subject: 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. --- compiler/rustc_parse/src/lexer/unescape_error_reporting.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'compiler/rustc_parse') diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs index 7f68112a427..a41956c58f0 100644 --- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs +++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs @@ -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 \ ", -- cgit 1.4.1-3-g733a5