about summary refs log tree commit diff
path: root/compiler/rustc_parse/src
diff options
context:
space:
mode:
authorclubby789 <jamie@hill-daniel.co.uk>2022-10-22 02:37:15 +0100
committerclubby789 <jamie@hill-daniel.co.uk>2022-10-22 02:37:15 +0100
commited40d461590ab0101ccec7610b8d8faecc94f7ee (patch)
treef42d6d22ad566b9439b576abbf3220057c30e97a /compiler/rustc_parse/src
parent0940040c0486a536be4f8685c7dd9a078f9e87c2 (diff)
downloadrust-ed40d461590ab0101ccec7610b8d8faecc94f7ee.tar.gz
rust-ed40d461590ab0101ccec7610b8d8faecc94f7ee.zip
Properly escape quotes when suggesting switching between char/string literals
Diffstat (limited to 'compiler/rustc_parse/src')
-rw-r--r--compiler/rustc_parse/src/lexer/unescape_error_reporting.rs19
1 files changed, 17 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
index 77c4fadab45..f075de71426 100644
--- a/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
+++ b/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
@@ -113,11 +113,26 @@ pub(crate) fn emit_unescape_error(
                 } else {
                     ("", "if you meant to write a `str` literal, use double quotes")
                 };
-
+                let mut escaped = String::with_capacity(lit.len());
+                let mut chrs = lit.chars().peekable();
+                while let Some(first) = chrs.next() {
+                    match (first, chrs.peek()) {
+                        ('\\', Some('"')) => {
+                            escaped.push('\\');
+                            escaped.push('"');
+                            chrs.next();
+                        }
+                        ('"', _) => {
+                            escaped.push('\\');
+                            escaped.push('"')
+                        }
+                        (c, _) => escaped.push(c),
+                    };
+                }
                 handler.span_suggestion(
                     span_with_quotes,
                     msg,
-                    format!("{}\"{}\"", prefix, lit),
+                    format!("{prefix}\"{escaped}\""),
                     Applicability::MachineApplicable,
                 );
             }