]> git.lizzy.rs Git - rust.git/commitdiff
get_hint_if_single_char_arg: fix bug where multi-char letters are not detected properly
authorMatthias Krüger <matthias.krueger@famsik.de>
Thu, 24 Sep 2020 14:36:29 +0000 (16:36 +0200)
committerEduardo Broto <ebroto@tutanota.com>
Fri, 30 Oct 2020 22:29:44 +0000 (23:29 +0100)
clippy_lints/src/methods/mod.rs
tests/ui/single_char_pattern.fixed
tests/ui/single_char_pattern.stderr
tests/ui/single_char_push_str.fixed
tests/ui/single_char_push_str.stderr

index 68a152270e05e31cc7e74c61a3c56a7188f2d7ea..5cc3c25e01de62ec9e696d58c6cbc16eabae4fe5 100644 (file)
@@ -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 {
index 3871c4f2268cc36e02c1c73916b2cbcaff9a5ffb..817a06199ffe598c671ff268c096c78d3c7746b2 100644 (file)
@@ -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');
index fe7211c53f8521bb6f155121b14bc0c69eb341f3..ecaabd9155bb58892041ecb4c5125d70684e6450 100644 (file)
@@ -6,6 +6,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
    |
@@ -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
 
index da742fe70e23ccb7ee5b27ce249101f5bec8df39..3c550bee9a38672b9f0cf0b04e181dc0043c449c 100644 (file)
@@ -19,5 +19,5 @@ fn main() {
     string.push('\u{0052}');
     string.push('a');
 
-    get_string!().push_str("ö");
+    get_string!().push('ö');
 }
index 1f53558912121835694fd6145285995c2905d6f0..d6e6e635cc5356b9d95aa7e30d509e41dcf97b17 100644 (file)
@@ -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