Rollup merge of #114913 - beetrees:escape-double-quote, r=davidtwco

Fix suggestion for attempting to define a string with single quotes

Currently attempting to compile `fn main() { let _ = '\\"'; }` will result in the following error message:
```
error: character literal may only contain one codepoint
 --> src/main.rs:1:21
  |
1 | fn main() { let _ = '\\"'; }
  |                     ^^^^^
  |
help: if you meant to write a `str` literal, use double quotes
  |
1 | fn main() { let _ = "\\""; }
  |                     ~~~~~
```
The suggestion is invalid as it fails to escape the `"`. This PR fixes the suggestion so that it now reads:
```
help: if you meant to write a `str` literal, use double quotes
  |
1 | fn main() { let _ = "\\\""; }
  |                     ~~~~~~
```
The relevant test is also updated to ensure that this does not regress in future.
This commit is contained in:
Josh Stone 2023-08-17 15:40:09 -07:00 committed by GitHub
commit 7ea4de9632
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 15 deletions

View File

@ -80,20 +80,14 @@ pub(crate) fn emit_unescape_error(
let sugg = sugg.unwrap_or_else(|| { let sugg = sugg.unwrap_or_else(|| {
let prefix = mode.prefix_noraw(); let prefix = mode.prefix_noraw();
let mut escaped = String::with_capacity(lit.len()); let mut escaped = String::with_capacity(lit.len());
let mut chrs = lit.chars().peekable(); let mut in_escape = false;
while let Some(first) = chrs.next() { for c in lit.chars() {
match (first, chrs.peek()) { match c {
('\\', Some('"')) => { '\\' => in_escape = !in_escape,
escaped.push('\\'); '"' if !in_escape => escaped.push('\\'),
escaped.push('"'); _ => in_escape = false,
chrs.next(); }
} escaped.push(c);
('"', _) => {
escaped.push('\\');
escaped.push('"')
}
(c, _) => escaped.push(c),
};
} }
let sugg = format!("{prefix}\"{escaped}\""); let sugg = format!("{prefix}\"{escaped}\"");
MoreThanOneCharSugg::Quotes { MoreThanOneCharSugg::Quotes {

View File

@ -7,4 +7,5 @@ fn main() {
let _: &str = "a"; //~ ERROR mismatched types let _: &str = "a"; //~ ERROR mismatched types
let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint
let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint let _: &str = "\"\"\""; //~ ERROR character literal may only contain one codepoint
let _: &str = "\"\"\\\"\\\"\\\\\""; //~ ERROR character literal may only contain one codepoint
} }

View File

@ -7,4 +7,5 @@ fn main() {
let _: &str = 'a'; //~ ERROR mismatched types let _: &str = 'a'; //~ ERROR mismatched types
let _: &str = '"""'; //~ ERROR character literal may only contain one codepoint let _: &str = '"""'; //~ ERROR character literal may only contain one codepoint
let _: &str = '\"\"\"'; //~ ERROR character literal may only contain one codepoint let _: &str = '\"\"\"'; //~ ERROR character literal may only contain one codepoint
let _: &str = '"\"\\"\\\"\\\\"'; //~ ERROR character literal may only contain one codepoint
} }

View File

@ -20,6 +20,17 @@ help: if you meant to write a `str` literal, use double quotes
LL | let _: &str = "\"\"\""; LL | let _: &str = "\"\"\"";
| ~~~~~~~~ | ~~~~~~~~
error: character literal may only contain one codepoint
--> $DIR/str-as-char.rs:10:19
|
LL | let _: &str = '"\"\"\\"\\"';
| ^^^^^^^^^^^^^^^^^
|
help: if you meant to write a `str` literal, use double quotes
|
LL | let _: &str = "\"\"\\"\\"\\\"";
| ~~~~~~~~~~~~~~~~~~~~
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/str-as-char.rs:7:19 --> $DIR/str-as-char.rs:7:19
| |
@ -33,6 +44,6 @@ help: if you meant to write a `str` literal, use double quotes
LL | let _: &str = "a"; LL | let _: &str = "a";
| ~~~ | ~~~
error: aborting due to 3 previous errors error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.