Skip to content

Commit fddef24

Browse files
committed
Auto merge of rust-lang#8067 - frobiac:8060-backslash_escaped_in_single_char_pattern, r=giraffate
Escape backslash in single_char_pattern.rs Escape backslash in single_char_pattern. Previously, the proposed clippy fix for a single backslash in raw strings ```r"\"``` or ```r#"\"#``` was also only an unescaped, *single* ```'\'```: ```shell warning: single-character string constant used as pattern 2 | let s = r#"abc\xyz/"#.find(r"\"); | ^^^^ help: try using a `char` instead: `'\'` | = note: `#[warn(clippy::single_char_pattern)]` on by default ``` This PR corrects this to a properly escaped *double* backslash ```'\\'```. I haven't come up with any other problematic cases, a single quote was already handled. Closes: rust-lang#8060 changelog: Escape backslash in ``[`single_char_pattern`]``
2 parents d5d830a + 5cc451b commit fddef24

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

clippy_lints/src/methods/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ pub(super) fn get_hint_if_single_char_arg(
6666
// for regular string: "a"
6767
&snip[1..(snip.len() - 1)]
6868
};
69-
let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
69+
70+
let hint = format!("'{}'", match ch {
71+
"'" => "\\'" ,
72+
r"\" => "\\\\",
73+
_ => ch,
74+
});
75+
7076
Some(hint)
7177
} else {
7278
None

tests/ui/single_char_pattern.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ fn main() {
5656
x.split('a');
5757
x.split('\'');
5858
x.split('#');
59+
// Must escape backslash in raw strings when converting to char #8060
60+
x.split('\\');
61+
x.split('\\');
5962
}

tests/ui/single_char_pattern.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ fn main() {
5656
x.split(r###"a"###);
5757
x.split(r###"'"###);
5858
x.split(r###"#"###);
59+
// Must escape backslash in raw strings when converting to char #8060
60+
x.split(r#"\"#);
61+
x.split(r"\");
5962
}

tests/ui/single_char_pattern.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,17 @@ error: single-character string constant used as pattern
192192
LL | x.split(r###"#"###);
193193
| ^^^^^^^^^^ help: try using a `char` instead: `'#'`
194194

195-
error: aborting due to 32 previous errors
195+
error: single-character string constant used as pattern
196+
--> $DIR/single_char_pattern.rs:60:13
197+
|
198+
LL | x.split(r#"/"#);
199+
| ^^^^^^ help: try using a `char` instead: `'/'`
200+
201+
error: single-character string constant used as pattern
202+
--> $DIR/single_char_pattern.rs:61:13
203+
|
204+
LL | x.split(r"/");
205+
| ^^^^ help: try using a `char` instead: `'/'`
206+
207+
error: aborting due to 34 previous errors
196208

0 commit comments

Comments
 (0)