1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_trait_method;
3 use clippy_utils::source::snippet;
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
8 use rustc_lint::LateContext;
10 use rustc_span::symbol::sym;
12 use super::MAP_COLLECT_RESULT_UNIT;
17 map_args: &[hir::Expr<'_>],
18 collect_args: &[hir::Expr<'_>],
22 if let [map_expr] = collect_args;
23 if is_trait_method(cx, map_expr, sym::Iterator);
24 // return of collect `Result<(),_>`
25 let collect_ret_ty = cx.typeck_results().expr_ty(expr);
26 if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
27 if let ty::Adt(_, substs) = collect_ret_ty.kind();
28 if let Some(result_t) = substs.types().next();
29 if result_t.is_unit();
30 // get parts for snippet
31 if let [iter, map_fn] = map_args;
35 MAP_COLLECT_RESULT_UNIT,
37 "`.map().collect()` can be replaced with `.try_for_each()`",
40 "{}.try_for_each({})",
41 snippet(cx, iter.span, ".."),
42 snippet(cx, map_fn.span, "..")
44 Applicability::MachineApplicable,