Skip to content

Commit

Permalink
Escape backslash for singe_char_pattern.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
frobiac committed Dec 2, 2021
1 parent d5d830a commit 5cc451b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion clippy_lints/src/methods/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/single_char_pattern.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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('\\');
}
3 changes: 3 additions & 0 deletions tests/ui/single_char_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"\");
}
14 changes: 13 additions & 1 deletion tests/ui/single_char_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5cc451b

Please sign in to comment.