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
This commit is contained in:
Matthias Krüger 2022-10-23 08:14:31 +02:00 committed by GitHub
commit b656f5e9a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 8 deletions

View file

@ -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,
);
}