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:
commit
b656f5e9a6
8 changed files with 83 additions and 8 deletions
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue