about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-08-09 07:58:39 +0000
committerbors <bors@rust-lang.org>2019-08-09 07:58:39 +0000
commit26a1e532e6ce2067b5c157af0157f3b18697781a (patch)
tree763d96c200af43d38a28ebedee0f7df584eaa57e
parent764d68749a7387d7b50fc4c67864b73a1adaba54 (diff)
parentb825cddb6d7b2ec7dc479f200047ddf43a01fea0 (diff)
downloadrust-26a1e532e6ce2067b5c157af0157f3b18697781a.tar.gz
rust-26a1e532e6ce2067b5c157af0157f3b18697781a.zip
Auto merge of #4361 - lzutao:fix-raw-string-on-single_char_pattern, r=phansch
Fix lint_single_char_pattern on raw string literal

Closes #4356
changelog: Handle raw string literal on `single_char_literal` lint.
-rw-r--r--clippy_lints/src/methods/mod.rs13
-rw-r--r--tests/ui/single_char_pattern.fixed7
-rw-r--r--tests/ui/single_char_pattern.rs7
-rw-r--r--tests/ui/single_char_pattern.stderr32
4 files changed, 55 insertions, 4 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index f40171aeed3..19151a414c7 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -2329,13 +2329,20 @@ fn lint_chars_last_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &
 fn lint_single_char_pattern<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, _expr: &'tcx hir::Expr, arg: &'tcx hir::Expr) {
     if_chain! {
         if let hir::ExprKind::Lit(lit) = &arg.node;
-        if let ast::LitKind::Str(r, _) = lit.node;
+        if let ast::LitKind::Str(r, style) = lit.node;
         if r.as_str().len() == 1;
         then {
             let mut applicability = Applicability::MachineApplicable;
             let snip = snippet_with_applicability(cx, arg.span, "..", &mut applicability);
-            let c = &snip[1..snip.len() - 1];
-            let hint = format!("'{}'", if c == "'" { "\\'" } else { c });
+            let ch = if let ast::StrStyle::Raw(nhash) = style {
+                let nhash = nhash as usize;
+                // for raw string: r##"a"##
+                &snip[(nhash + 2)..(snip.len() - 1 - nhash)]
+            } else {
+                // for regular string: "a"
+                &snip[1..(snip.len() - 1)]
+            };
+            let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
             span_lint_and_sugg(
                 cx,
                 SINGLE_CHAR_PATTERN,
diff --git a/tests/ui/single_char_pattern.fixed b/tests/ui/single_char_pattern.fixed
index d5b6d81d0a5..3871c4f2268 100644
--- a/tests/ui/single_char_pattern.fixed
+++ b/tests/ui/single_char_pattern.fixed
@@ -53,4 +53,11 @@ fn main() {
     // Issue #3204
     const S: &str = "#";
     x.find(S);
+
+    // Raw string
+    x.split('a');
+    x.split('a');
+    x.split('a');
+    x.split('\'');
+    x.split('#');
 }
diff --git a/tests/ui/single_char_pattern.rs b/tests/ui/single_char_pattern.rs
index 73f364854d0..32afe339cd8 100644
--- a/tests/ui/single_char_pattern.rs
+++ b/tests/ui/single_char_pattern.rs
@@ -53,4 +53,11 @@ fn main() {
     // Issue #3204
     const S: &str = "#";
     x.find(S);
+
+    // Raw string
+    x.split(r"a");
+    x.split(r#"a"#);
+    x.split(r###"a"###);
+    x.split(r###"'"###);
+    x.split(r###"#"###);
 }
diff --git a/tests/ui/single_char_pattern.stderr b/tests/ui/single_char_pattern.stderr
index d394c989c44..464ed375976 100644
--- a/tests/ui/single_char_pattern.stderr
+++ b/tests/ui/single_char_pattern.stderr
@@ -132,5 +132,35 @@ error: single-character string constant used as pattern
 LL |     x.starts_with("/x03"); // issue #2996
    |                   ^^^^^^ help: try using a char instead: `'/x03'`
 
-error: aborting due to 22 previous errors
+error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:58:13
+   |
+LL |     x.split(r"a");
+   |             ^^^^ help: try using a char instead: `'a'`
+
+error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:59:13
+   |
+LL |     x.split(r#"a"#);
+   |             ^^^^^^ help: try using a char instead: `'a'`
+
+error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:60:13
+   |
+LL |     x.split(r###"a"###);
+   |             ^^^^^^^^^^ help: try using a char instead: `'a'`
+
+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: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:62:13
+   |
+LL |     x.split(r###"#"###);
+   |             ^^^^^^^^^^ help: try using a char instead: `'#'`
+
+error: aborting due to 27 previous errors