1 use crate::utils::usage::mutated_variables;
2 use crate::utils::{match_qpath, match_trait_method, path_to_local_id, paths, span_lint};
4 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc_lint::LateContext;
6 use rustc_middle::hir::map::Map;
8 use if_chain::if_chain;
10 use super::UNNECESSARY_FILTER_MAP;
12 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
13 if !match_trait_method(cx, expr, &paths::ITERATOR) {
17 if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].kind {
18 let body = cx.tcx.hir().body(body_id);
19 let arg_id = body.params[0].pat.hir_id;
21 mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
23 let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
25 let mut return_visitor = ReturnVisitor::new(&cx, arg_id);
26 return_visitor.visit_expr(&body.value);
27 found_mapping |= return_visitor.found_mapping;
28 found_filtering |= return_visitor.found_filtering;
33 UNNECESSARY_FILTER_MAP,
35 "this `.filter_map` can be written more simply using `.map`",
40 if !found_mapping && !mutates_arg {
43 UNNECESSARY_FILTER_MAP,
45 "this `.filter_map` can be written more simply using `.filter`",
52 // returns (found_mapping, found_filtering)
53 fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
55 hir::ExprKind::Call(ref func, ref args) => {
57 if let hir::ExprKind::Path(ref path) = func.kind;
59 if match_qpath(path, &paths::OPTION_SOME) {
60 if path_to_local_id(&args[0], arg_id) {
65 // We don't know. It might do anything.
71 hir::ExprKind::Block(ref block, _) => block
74 .map_or((false, false), |expr| check_expression(cx, arg_id, &expr)),
75 hir::ExprKind::Match(_, arms, _) => {
76 let mut found_mapping = false;
77 let mut found_filtering = false;
79 let (m, f) = check_expression(cx, arg_id, &arm.body);
83 (found_mapping, found_filtering)
85 // There must be an else_arm or there will be a type error
86 hir::ExprKind::If(_, ref if_arm, Some(ref else_arm)) => {
87 let if_check = check_expression(cx, arg_id, if_arm);
88 let else_check = check_expression(cx, arg_id, else_arm);
89 (if_check.0 | else_check.0, if_check.1 | else_check.1)
91 hir::ExprKind::Path(path) if match_qpath(path, &paths::OPTION_NONE) => (false, true),
96 struct ReturnVisitor<'a, 'tcx> {
97 cx: &'a LateContext<'tcx>,
99 // Found a non-None return that isn't Some(input)
101 // Found a return that isn't Some
102 found_filtering: bool,
105 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
106 fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
110 found_mapping: false,
111 found_filtering: false,
116 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
117 type Map = Map<'tcx>;
119 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
120 if let hir::ExprKind::Ret(Some(expr)) = &expr.kind {
121 let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
122 self.found_mapping |= found_mapping;
123 self.found_filtering |= found_filtering;
125 walk_expr(self, expr);
129 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
130 NestedVisitorMap::None