diff options
| author | bors <bors@rust-lang.org> | 2021-12-03 00:23:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-12-03 00:23:59 +0000 |
| commit | fddef249a23018737b27b91e33129a0ac8a400ce (patch) | |
| tree | 49b806b1f0eeccd9bc6af3d6dbdbed4483eb0f05 | |
| parent | d5d830a50f0a0211bd0820aa8403df8d47f109c3 (diff) | |
| parent | 5cc451bc6cb9fc74c85e44be4a510e9c14f03914 (diff) | |
| download | rust-fddef249a23018737b27b91e33129a0ac8a400ce.tar.gz rust-fddef249a23018737b27b91e33129a0ac8a400ce.zip | |
Auto merge of #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: #8060 changelog: Escape backslash in ``[`single_char_pattern`]``
| -rw-r--r-- | clippy_lints/src/methods/utils.rs | 8 | ||||
| -rw-r--r-- | tests/ui/single_char_pattern.fixed | 3 | ||||
| -rw-r--r-- | tests/ui/single_char_pattern.rs | 3 | ||||
| -rw-r--r-- | tests/ui/single_char_pattern.stderr | 14 |
4 files changed, 26 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/utils.rs b/clippy_lints/src/methods/utils.rs index ba2ce73a116..11ad881ee7b 100644 --- a/clippy_lints/src/methods/utils.rs +++ b/clippy_lints/src/methods/utils.rs @@ -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 diff --git a/tests/ui/single_char_pattern.fixed b/tests/ui/single_char_pattern.fixed index 1abd2b7883d..ea26defd3d5 100644 --- a/tests/ui/single_char_pattern.fixed +++ b/tests/ui/single_char_pattern.fixed @@ -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('\\'); } diff --git a/tests/ui/single_char_pattern.rs b/tests/ui/single_char_pattern.rs index e662bf34be2..eb2d0274fa2 100644 --- a/tests/ui/single_char_pattern.rs +++ b/tests/ui/single_char_pattern.rs @@ -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"\"); } diff --git a/tests/ui/single_char_pattern.stderr b/tests/ui/single_char_pattern.stderr index 22d4b2d460f..f438d3da8ee 100644 --- a/tests/ui/single_char_pattern.stderr +++ b/tests/ui/single_char_pattern.stderr @@ -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 |
