about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2021-03-04 19:47:06 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2021-03-11 19:37:16 +0900
commitdb91d9cf9a620cc258ab84a152a9333351ac166e (patch)
tree458691d2f7c1b4814b7a313be6f9735777683132
parent9b0a42b67d66cb79bdd09379d0b0e7959fc1505a (diff)
downloadrust-db91d9cf9a620cc258ab84a152a9333351ac166e.tar.gz
rust-db91d9cf9a620cc258ab84a152a9333351ac166e.zip
move map_collect_result_unit to its own module
-rw-r--r--clippy_lints/src/methods/map_collect_result_unit.rs45
-rw-r--r--clippy_lints/src/methods/mod.rs39
2 files changed, 47 insertions, 37 deletions
diff --git a/clippy_lints/src/methods/map_collect_result_unit.rs b/clippy_lints/src/methods/map_collect_result_unit.rs
new file mode 100644
index 00000000000..5b20e268d9f
--- /dev/null
+++ b/clippy_lints/src/methods/map_collect_result_unit.rs
@@ -0,0 +1,45 @@
+use crate::utils::{is_type_diagnostic_item, match_trait_method, paths, snippet, 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::sym;
+
+use super::MAP_COLLECT_RESULT_UNIT;
+
+pub(super) fn check(
+    cx: &LateContext<'_>,
+    expr: &hir::Expr<'_>,
+    map_args: &[hir::Expr<'_>],
+    collect_args: &[hir::Expr<'_>],
+) {
+    if_chain! {
+        // called on Iterator
+        if let [map_expr] = collect_args;
+        if match_trait_method(cx, map_expr, &paths::ITERATOR);
+        // return of collect `Result<(),_>`
+        let collect_ret_ty = cx.typeck_results().expr_ty(expr);
+        if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
+        if let ty::Adt(_, substs) = collect_ret_ty.kind();
+        if let Some(result_t) = substs.types().next();
+        if result_t.is_unit();
+        // get parts for snippet
+        if let [iter, map_fn] = map_args;
+        then {
+            span_lint_and_sugg(
+                cx,
+                MAP_COLLECT_RESULT_UNIT,
+                expr.span,
+                "`.map().collect()` can be replaced with `.try_for_each()`",
+                "try this",
+                format!(
+                    "{}.try_for_each({})",
+                    snippet(cx, iter.span, ".."),
+                    snippet(cx, map_fn.span, "..")
+                ),
+                Applicability::MachineApplicable,
+            );
+        }
+    }
+}
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index c944568dc61..baa342ab3bb 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -11,6 +11,7 @@ mod inspect_for_each;
 mod iter_cloned_collect;
 mod iter_count;
 mod manual_saturating_arithmetic;
+mod map_collect_result_unit;
 mod ok_expect;
 mod option_as_ref_deref;
 mod option_map_unwrap_or;
@@ -1739,7 +1740,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
             ["unwrap_or_else", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "unwrap_or"),
             ["get_or_insert_with", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "get_or_insert"),
             ["ok_or_else", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "ok_or"),
-            ["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]),
+            ["collect", "map"] => map_collect_result_unit::check(cx, expr, arg_lists[1], arg_lists[0]),
             ["for_each", "inspect"] => inspect_for_each::check(cx, expr, method_spans[1]),
             ["to_owned", ..] => implicit_clone::check(cx, expr, sym::ToOwned),
             ["to_os_string", ..] => implicit_clone::check(cx, expr, sym::OsStr),
@@ -3530,42 +3531,6 @@ fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_
     }
 }
 
-fn lint_map_collect(
-    cx: &LateContext<'_>,
-    expr: &hir::Expr<'_>,
-    map_args: &[hir::Expr<'_>],
-    collect_args: &[hir::Expr<'_>],
-) {
-    if_chain! {
-        // called on Iterator
-        if let [map_expr] = collect_args;
-        if match_trait_method(cx, map_expr, &paths::ITERATOR);
-        // return of collect `Result<(),_>`
-        let collect_ret_ty = cx.typeck_results().expr_ty(expr);
-        if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
-        if let ty::Adt(_, substs) = collect_ret_ty.kind();
-        if let Some(result_t) = substs.types().next();
-        if result_t.is_unit();
-        // get parts for snippet
-        if let [iter, map_fn] = map_args;
-        then {
-            span_lint_and_sugg(
-                cx,
-                MAP_COLLECT_RESULT_UNIT,
-                expr.span,
-                "`.map().collect()` can be replaced with `.try_for_each()`",
-                "try this",
-                format!(
-                    "{}.try_for_each({})",
-                    snippet(cx, iter.span, ".."),
-                    snippet(cx, map_fn.span, "..")
-                ),
-                Applicability::MachineApplicable,
-            );
-        }
-    }
-}
-
 enum Convention {
     Eq(&'static str),
     StartsWith(&'static str),