1 use crate::utils::{match_trait_method, path_to_local_id, paths, snippet, span_lint_and_sugg, SpanlessEq};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
5 use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::TyS;
8 use rustc_span::symbol::sym;
10 use super::MANUAL_FILTER_MAP;
11 use super::MANUAL_FIND_MAP;
13 /// lint use of `filter().map()` or `find().map()` for `Iterators`
14 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, is_find: bool) {
16 if let ExprKind::MethodCall(_, _, [map_recv, map_arg], map_span) = expr.kind;
17 if let ExprKind::MethodCall(_, _, [_, filter_arg], filter_span) = map_recv.kind;
18 if match_trait_method(cx, map_recv, &paths::ITERATOR);
20 // filter(|x| ...is_some())...
21 if let ExprKind::Closure(_, _, filter_body_id, ..) = filter_arg.kind;
22 let filter_body = cx.tcx.hir().body(filter_body_id);
23 if let [filter_param] = filter_body.params;
24 // optional ref pattern: `filter(|&x| ..)`
25 let (filter_pat, is_filter_param_ref) = if let PatKind::Ref(ref_pat, _) = filter_param.pat.kind {
28 (filter_param.pat, false)
30 // closure ends with is_some() or is_ok()
31 if let PatKind::Binding(_, filter_param_id, _, None) = filter_pat.kind;
32 if let ExprKind::MethodCall(path, _, [filter_arg], _) = filter_body.value.kind;
33 if let Some(opt_ty) = cx.typeck_results().expr_ty(filter_arg).ty_adt_def();
34 if let Some(is_result) = if cx.tcx.is_diagnostic_item(sym::option_type, opt_ty.did) {
36 } else if cx.tcx.is_diagnostic_item(sym::result_type, opt_ty.did) {
41 if path.ident.name.as_str() == if is_result { "is_ok" } else { "is_some" };
43 // ...map(|x| ...unwrap())
44 if let ExprKind::Closure(_, _, map_body_id, ..) = map_arg.kind;
45 let map_body = cx.tcx.hir().body(map_body_id);
46 if let [map_param] = map_body.params;
47 if let PatKind::Binding(_, map_param_id, map_param_ident, None) = map_param.pat.kind;
48 // closure ends with expect() or unwrap()
49 if let ExprKind::MethodCall(seg, _, [map_arg, ..], _) = map_body.value.kind;
50 if matches!(seg.ident.name, sym::expect | sym::unwrap | sym::unwrap_or);
52 let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
53 // in `filter(|x| ..)`, replace `*x` with `x`
54 let a_path = if_chain! {
55 if !is_filter_param_ref;
56 if let ExprKind::Unary(UnOp::Deref, expr_path) = a.kind;
57 then { expr_path } else { a }
59 // let the filter closure arg and the map closure arg be equal
61 if path_to_local_id(a_path, filter_param_id);
62 if path_to_local_id(b, map_param_id);
63 if TyS::same_type(cx.typeck_results().expr_ty_adjusted(a), cx.typeck_results().expr_ty_adjusted(b));
70 if SpanlessEq::new(cx).expr_fallback(eq_fallback).eq_expr(filter_arg, map_arg);
72 let span = filter_span.to(map_span);
73 let (filter_name, lint) = if is_find {
74 ("find", MANUAL_FIND_MAP)
76 ("filter", MANUAL_FILTER_MAP)
78 let msg = format!("`{}(..).map(..)` can be simplified as `{0}_map(..)`", filter_name);
79 let to_opt = if is_result { ".ok()" } else { "" };
80 let sugg = format!("{}_map(|{}| {}{})", filter_name, map_param_ident,
81 snippet(cx, map_arg.span, ".."), to_opt);
82 span_lint_and_sugg(cx, lint, span, &msg, "try", sugg, Applicability::MachineApplicable);