about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2020-09-24 16:36:29 +0200
committerEduardo Broto <ebroto@tutanota.com>2020-10-30 23:29:44 +0100
commitc1eb8ceede6538590a39721c65ee2f943ceffe40 (patch)
tree64bef69c76338e3373ae5d0a9ad573cec1182793
parentc6412aeebc432ab49d46fe1f45a0fb5322c805d1 (diff)
downloadrust-c1eb8ceede6538590a39721c65ee2f943ceffe40.tar.gz
rust-c1eb8ceede6538590a39721c65ee2f943ceffe40.zip
get_hint_if_single_char_arg: fix bug where multi-char letters are not detected properly
-rw-r--r--clippy_lints/src/methods/mod.rs2
-rw-r--r--tests/ui/single_char_pattern.fixed6
-rw-r--r--tests/ui/single_char_pattern.stderr20
-rw-r--r--tests/ui/single_char_push_str.fixed2
-rw-r--r--tests/ui/single_char_push_str.stderr8
5 files changed, 31 insertions, 7 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 68a152270e0..5cc3c25e01d 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -3204,7 +3204,7 @@ fn get_hint_if_single_char_arg(
         if let hir::ExprKind::Lit(lit) = &arg.kind;
         if let ast::LitKind::Str(r, style) = lit.node;
         let string = r.as_str();
-        if string.len() == 1;
+        if string.chars().count() == 1;
         then {
             let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
             let ch = if let ast::StrStyle::Raw(nhash) = style {
diff --git a/tests/ui/single_char_pattern.fixed b/tests/ui/single_char_pattern.fixed
index 3871c4f2268..817a06199ff 100644
--- a/tests/ui/single_char_pattern.fixed
+++ b/tests/ui/single_char_pattern.fixed
@@ -18,9 +18,9 @@ fn main() {
     //
     // We may not want to suggest changing these anyway
     // See: https://github.com/rust-lang/rust-clippy/issues/650#issuecomment-184328984
-    x.split("ß");
-    x.split("ℝ");
-    x.split("💣");
+    x.split('ß');
+    x.split('ℝ');
+    x.split('💣');
     // Can't use this lint for unicode code points which don't fit in a char
     x.split("❤️");
     x.contains('x');
diff --git a/tests/ui/single_char_pattern.stderr b/tests/ui/single_char_pattern.stderr
index fe7211c53f8..ecaabd9155b 100644
--- a/tests/ui/single_char_pattern.stderr
+++ b/tests/ui/single_char_pattern.stderr
@@ -7,6 +7,24 @@ LL |     x.split("x");
    = note: `-D clippy::single-char-pattern` implied by `-D warnings`
 
 error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:21:13
+   |
+LL |     x.split("ß");
+   |             ^^^ help: try using a `char` instead: `'ß'`
+
+error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:22:13
+   |
+LL |     x.split("ℝ");
+   |             ^^^ help: try using a `char` instead: `'ℝ'`
+
+error: single-character string constant used as pattern
+  --> $DIR/single_char_pattern.rs:23:13
+   |
+LL |     x.split("💣");
+   |             ^^^^ help: try using a `char` instead: `'💣'`
+
+error: single-character string constant used as pattern
   --> $DIR/single_char_pattern.rs:26:16
    |
 LL |     x.contains("x");
@@ -162,5 +180,5 @@ error: single-character string constant used as pattern
 LL |     x.split(r###"#"###);
    |             ^^^^^^^^^^ help: try using a `char` instead: `'#'`
 
-error: aborting due to 27 previous errors
+error: aborting due to 30 previous errors
 
diff --git a/tests/ui/single_char_push_str.fixed b/tests/ui/single_char_push_str.fixed
index da742fe70e2..3c550bee9a3 100644
--- a/tests/ui/single_char_push_str.fixed
+++ b/tests/ui/single_char_push_str.fixed
@@ -19,5 +19,5 @@ fn main() {
     string.push('\u{0052}');
     string.push('a');
 
-    get_string!().push_str("ö");
+    get_string!().push('ö');
 }
diff --git a/tests/ui/single_char_push_str.stderr b/tests/ui/single_char_push_str.stderr
index 1f535589121..d6e6e635cc5 100644
--- a/tests/ui/single_char_push_str.stderr
+++ b/tests/ui/single_char_push_str.stderr
@@ -30,5 +30,11 @@ error: calling `push_str()` using a single-character string literal
 LL |     string.push_str(r##"a"##);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `string.push('a')`
 
-error: aborting due to 5 previous errors
+error: calling `push_str()` using a single-character string literal
+  --> $DIR/single_char_push_str.rs:22:5
+   |
+LL |     get_string!().push_str("ö");
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `get_string!().push('ö')`
+
+error: aborting due to 6 previous errors