about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2021-03-17 14:26:27 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2021-03-18 00:59:44 +0900
commit4d1f2bc56519a9b37daaec84273f97e3aba4318b (patch)
tree25e43f37baddea0d2f94f561dffa2e095774d309
parent7a7fcc0eda1d62f44ec374bad3d14226888238ad (diff)
downloadrust-4d1f2bc56519a9b37daaec84273f97e3aba4318b.tar.gz
rust-4d1f2bc56519a9b37daaec84273f97e3aba4318b.zip
extract conditions for single_char_pattern into its own module
-rw-r--r--clippy_lints/src/methods/mod.rs12
-rw-r--r--clippy_lints/src/methods/single_char_pattern.rs36
2 files changed, 25 insertions, 23 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index e8cefbd7422..b97dee27c52 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -1787,17 +1787,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
                 inefficient_to_string::check(cx, expr, method_call.ident.name, args);
                 single_char_add_str::check(cx, expr, args);
                 into_iter_on_ref::check(cx, expr, *method_span, method_call.ident.name, args);
-
-                match cx.typeck_results().expr_ty_adjusted(&args[0]).kind() {
-                    ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
-                        for &(method, pos) in &PATTERN_METHODS {
-                            if method_call.ident.name.as_str() == method && args.len() > pos {
-                                single_char_pattern::check(cx, expr, &args[pos]);
-                            }
-                        }
-                    },
-                    _ => (),
-                }
+                single_char_pattern::check(cx, expr, method_call.ident.name, args);
             },
             hir::ExprKind::Binary(op, ref lhs, ref rhs)
                 if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne =>
diff --git a/clippy_lints/src/methods/single_char_pattern.rs b/clippy_lints/src/methods/single_char_pattern.rs
index 12d6418d700..ae998468c2d 100644
--- a/clippy_lints/src/methods/single_char_pattern.rs
+++ b/clippy_lints/src/methods/single_char_pattern.rs
@@ -1,23 +1,35 @@
 use crate::methods::get_hint_if_single_char_arg;
 use clippy_utils::diagnostics::span_lint_and_sugg;
+use if_chain::if_chain;
 use rustc_errors::Applicability;
 use rustc_hir as hir;
 use rustc_lint::LateContext;
+use rustc_middle::ty;
+use rustc_span::symbol::Symbol;
 
 use super::SINGLE_CHAR_PATTERN;
 
 /// lint for length-1 `str`s for methods in `PATTERN_METHODS`
-pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
-    let mut applicability = Applicability::MachineApplicable;
-    if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability) {
-        span_lint_and_sugg(
-            cx,
-            SINGLE_CHAR_PATTERN,
-            arg.span,
-            "single-character string constant used as pattern",
-            "try using a `char` instead",
-            hint,
-            applicability,
-        );
+pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
+    for &(method, pos) in &crate::methods::PATTERN_METHODS {
+        if_chain! {
+            if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(&args[0]).kind();
+            if *ty.kind() == ty::Str;
+            if method_name.as_str() == method && args.len() > pos;
+            let arg = &args[pos];
+            let mut applicability = Applicability::MachineApplicable;
+            if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability);
+            then {
+                span_lint_and_sugg(
+                    cx,
+                    SINGLE_CHAR_PATTERN,
+                    arg.span,
+                    "single-character string constant used as pattern",
+                    "try using a `char` instead",
+                    hint,
+                    applicability,
+                );
+            }
+        }
     }
 }