-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmap_collect_result_unit.rs
More file actions
35 lines (33 loc) · 1.13 KB
/
map_collect_result_unit.rs
File metadata and controls
35 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::res::MaybeDef;
use clippy_utils::source::snippet;
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<'_>, iter: &hir::Expr<'_>, map_fn: &hir::Expr<'_>) {
// return of collect `Result<(),_>`
let collect_ret_ty = cx.typeck_results().expr_ty(expr);
if collect_ret_ty.is_diag_item(cx, sym::Result)
&& let ty::Adt(_, args) = collect_ret_ty.kind()
&& let Some(result_t) = args.types().next()
&& result_t.is_unit()
// get parts for snippet
{
span_lint_and_sugg(
cx,
MAP_COLLECT_RESULT_UNIT,
expr.span,
"`.map().collect()` can be replaced with `.try_for_each()`",
"try",
format!(
"{}.try_for_each({})",
snippet(cx, iter.span, ".."),
snippet(cx, map_fn.span, "..")
),
Applicability::MachineApplicable,
);
}
}