1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
7 use rustc_lint::LateContext;
9 use rustc_span::symbol::sym;
11 use super::MAP_COLLECT_RESULT_UNIT;
13 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, iter: &hir::Expr<'_>, map_fn: &hir::Expr<'_>) {
14 // return of collect `Result<(),_>`
15 let collect_ret_ty = cx.typeck_results().expr_ty(expr);
17 if is_type_diagnostic_item(cx, collect_ret_ty, sym::Result);
18 if let ty::Adt(_, substs) = collect_ret_ty.kind();
19 if let Some(result_t) = substs.types().next();
20 if result_t.is_unit();
21 // get parts for snippet
25 MAP_COLLECT_RESULT_UNIT,
27 "`.map().collect()` can be replaced with `.try_for_each()`",
30 "{}.try_for_each({})",
31 snippet(cx, iter.span, ".."),
32 snippet(cx, map_fn.span, "..")
34 Applicability::MachineApplicable,