diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-10-23 08:14:31 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-23 08:14:31 +0200 |
| commit | b656f5e9a64d6fe0f0b75e76e6350fb08cea562d (patch) | |
| tree | 649aff2c2c3392d57c0c43afb76d563945fb534c /compiler/rustc_parse/src | |
| parent | 1a077420f864868ec0c8b5d401a4dbb60f18c77d (diff) | |
| parent | 876248506c9dcb0f4f57db49021374bb0bfc1bd7 (diff) | |
| download | rust-b656f5e9a64d6fe0f0b75e76e6350fb08cea562d.tar.gz rust-b656f5e9a64d6fe0f0b75e76e6350fb08cea562d.zip | |
Rollup merge of #103354 - clubby789:escape-string-literals, r=compiler-errors
Escape string literals when fixing overlong char literal Fixes #103323 ````@rustbot```` label +A-diagnostics +A-suggestion-diagnostics
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/lexer/unescape_error_reporting.rs | 19 |
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, ); } |
