From 5cc451bc6cb9fc74c85e44be4a510e9c14f03914 Mon Sep 17 00:00:00 2001 From: frobiac Date: Thu, 2 Dec 2021 23:47:23 +0100 Subject: [PATCH] Escape backslash for singe_char_pattern.rs --- clippy_lints/src/methods/utils.rs | 8 +++++++- tests/ui/single_char_pattern.fixed | 3 +++ tests/ui/single_char_pattern.rs | 3 +++ 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 ba2ce73a1165..11ad881ee7b9 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 1abd2b7883df..ea26defd3d57 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 e662bf34be2c..eb2d0274fa20 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 22d4b2d460fb..f438d3da8ee4 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