Skip to content

Commit 62a1119

Browse files
committed
move map_collect_result_unit to its own module
1 parent 27ec2df commit 62a1119

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::utils::{is_type_diagnostic_item, match_trait_method, paths, snippet, span_lint_and_sugg};
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_hir as hir;
5+
use rustc_lint::LateContext;
6+
use rustc_middle::ty;
7+
use rustc_span::symbol::sym;
8+
9+
use super::MAP_COLLECT_RESULT_UNIT;
10+
11+
pub(super) fn check(
12+
cx: &LateContext<'_>,
13+
expr: &hir::Expr<'_>,
14+
map_args: &[hir::Expr<'_>],
15+
collect_args: &[hir::Expr<'_>],
16+
) {
17+
if_chain! {
18+
// called on Iterator
19+
if let [map_expr] = collect_args;
20+
if match_trait_method(cx, map_expr, &paths::ITERATOR);
21+
// return of collect `Result<(),_>`
22+
let collect_ret_ty = cx.typeck_results().expr_ty(expr);
23+
if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
24+
if let ty::Adt(_, substs) = collect_ret_ty.kind();
25+
if let Some(result_t) = substs.types().next();
26+
if result_t.is_unit();
27+
// get parts for snippet
28+
if let [iter, map_fn] = map_args;
29+
then {
30+
span_lint_and_sugg(
31+
cx,
32+
MAP_COLLECT_RESULT_UNIT,
33+
expr.span,
34+
"`.map().collect()` can be replaced with `.try_for_each()`",
35+
"try this",
36+
format!(
37+
"{}.try_for_each({})",
38+
snippet(cx, iter.span, ".."),
39+
snippet(cx, map_fn.span, "..")
40+
),
41+
Applicability::MachineApplicable,
42+
);
43+
}
44+
}
45+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod inspect_for_each;
1111
mod iter_cloned_collect;
1212
mod iter_count;
1313
mod manual_saturating_arithmetic;
14+
mod map_collect_result_unit;
1415
mod ok_expect;
1516
mod option_as_ref_deref;
1617
mod option_map_unwrap_or;
@@ -1739,7 +1740,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17391740
["unwrap_or_else", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "unwrap_or"),
17401741
["get_or_insert_with", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "get_or_insert"),
17411742
["ok_or_else", ..] => unnecessary_lazy_eval::check(cx, expr, arg_lists[0], "ok_or"),
1742-
["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]),
1743+
["collect", "map"] => map_collect_result_unit::check(cx, expr, arg_lists[1], arg_lists[0]),
17431744
["for_each", "inspect"] => inspect_for_each::check(cx, expr, method_spans[1]),
17441745
["to_owned", ..] => implicit_clone::check(cx, expr, sym::ToOwned),
17451746
["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<'_
35303531
}
35313532
}
35323533

3533-
fn lint_map_collect(
3534-
cx: &LateContext<'_>,
3535-
expr: &hir::Expr<'_>,
3536-
map_args: &[hir::Expr<'_>],
3537-
collect_args: &[hir::Expr<'_>],
3538-
) {
3539-
if_chain! {
3540-
// called on Iterator
3541-
if let [map_expr] = collect_args;
3542-
if match_trait_method(cx, map_expr, &paths::ITERATOR);
3543-
// return of collect `Result<(),_>`
3544-
let collect_ret_ty = cx.typeck_results().expr_ty(expr);
3545-
if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
3546-
if let ty::Adt(_, substs) = collect_ret_ty.kind();
3547-
if let Some(result_t) = substs.types().next();
3548-
if result_t.is_unit();
3549-
// get parts for snippet
3550-
if let [iter, map_fn] = map_args;
3551-
then {
3552-
span_lint_and_sugg(
3553-
cx,
3554-
MAP_COLLECT_RESULT_UNIT,
3555-
expr.span,
3556-
"`.map().collect()` can be replaced with `.try_for_each()`",
3557-
"try this",
3558-
format!(
3559-
"{}.try_for_each({})",
3560-
snippet(cx, iter.span, ".."),
3561-
snippet(cx, map_fn.span, "..")
3562-
),
3563-
Applicability::MachineApplicable,
3564-
);
3565-
}
3566-
}
3567-
}
3568-
35693534
enum Convention {
35703535
Eq(&'static str),
35713536
StartsWith(&'static str),

0 commit comments

Comments
 (0)