Escape backslash for singe_char_pattern.rs

This commit is contained in:
frobiac 2021-12-02 23:47:23 +01:00
parent d5d830a50f
commit 5cc451bc6c
4 changed files with 26 additions and 2 deletions

View file

@ -66,7 +66,13 @@ pub(super) fn get_hint_if_single_char_arg(
// for regular string: "a"
&snip[1..(snip.len() - 1)]
};
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
let hint = format!("'{}'", match ch {
"'" => "\\'" ,
r"\" => "\\\\",
_ => ch,
});
Some(hint)
} else {
None

View file

@ -56,4 +56,7 @@ fn main() {
x.split('a');
x.split('\'');
x.split('#');
// Must escape backslash in raw strings when converting to char #8060
x.split('\\');
x.split('\\');
}

View file

@ -56,4 +56,7 @@ fn main() {
x.split(r###"a"###);
x.split(r###"'"###);
x.split(r###"#"###);
// Must escape backslash in raw strings when converting to char #8060
x.split(r#"\"#);
x.split(r"\");
}

View file

@ -192,5 +192,17 @@ error: single-character string constant used as pattern
LL | x.split(r###"#"###);
| ^^^^^^^^^^ help: try using a `char` instead: `'#'`
error: aborting due to 32 previous errors
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:60:13
|
LL | x.split(r#"/"#);
| ^^^^^^ help: try using a `char` instead: `'/'`
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:61:13
|
LL | x.split(r"/");
| ^^^^ help: try using a `char` instead: `'/'`
error: aborting due to 34 previous errors