]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/unnecessary_filter_map.rs
Destructure args in methods module
[rust.git] / clippy_lints / src / methods / unnecessary_filter_map.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::usage::mutated_variables;
3 use clippy_utils::{is_trait_method, match_qpath, path_to_local_id, paths};
4 use if_chain::if_chain;
5 use rustc_hir as hir;
6 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
7 use rustc_lint::LateContext;
8 use rustc_middle::hir::map::Map;
9 use rustc_span::sym;
10
11 use super::UNNECESSARY_FILTER_MAP;
12
13 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
14     if !is_trait_method(cx, expr, sym::Iterator) {
15         return;
16     }
17
18     if let hir::ExprKind::Closure(_, _, body_id, ..) = arg.kind {
19         let body = cx.tcx.hir().body(body_id);
20         let arg_id = body.params[0].pat.hir_id;
21         let mutates_arg =
22             mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
23
24         let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
25
26         let mut return_visitor = ReturnVisitor::new(&cx, arg_id);
27         return_visitor.visit_expr(&body.value);
28         found_mapping |= return_visitor.found_mapping;
29         found_filtering |= return_visitor.found_filtering;
30
31         if !found_filtering {
32             span_lint(
33                 cx,
34                 UNNECESSARY_FILTER_MAP,
35                 expr.span,
36                 "this `.filter_map` can be written more simply using `.map`",
37             );
38             return;
39         }
40
41         if !found_mapping && !mutates_arg {
42             span_lint(
43                 cx,
44                 UNNECESSARY_FILTER_MAP,
45                 expr.span,
46                 "this `.filter_map` can be written more simply using `.filter`",
47             );
48             return;
49         }
50     }
51 }
52
53 // returns (found_mapping, found_filtering)
54 fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
55     match &expr.kind {
56         hir::ExprKind::Call(ref func, ref args) => {
57             if_chain! {
58                 if let hir::ExprKind::Path(ref path) = func.kind;
59                 then {
60                     if match_qpath(path, &paths::OPTION_SOME) {
61                         if path_to_local_id(&args[0], arg_id) {
62                             return (false, false)
63                         }
64                         return (true, false);
65                     }
66                     // We don't know. It might do anything.
67                     return (true, true);
68                 }
69             }
70             (true, true)
71         },
72         hir::ExprKind::Block(ref block, _) => block
73             .expr
74             .as_ref()
75             .map_or((false, false), |expr| check_expression(cx, arg_id, &expr)),
76         hir::ExprKind::Match(_, arms, _) => {
77             let mut found_mapping = false;
78             let mut found_filtering = false;
79             for arm in *arms {
80                 let (m, f) = check_expression(cx, arg_id, &arm.body);
81                 found_mapping |= m;
82                 found_filtering |= f;
83             }
84             (found_mapping, found_filtering)
85         },
86         // There must be an else_arm or there will be a type error
87         hir::ExprKind::If(_, ref if_arm, Some(ref else_arm)) => {
88             let if_check = check_expression(cx, arg_id, if_arm);
89             let else_check = check_expression(cx, arg_id, else_arm);
90             (if_check.0 | else_check.0, if_check.1 | else_check.1)
91         },
92         hir::ExprKind::Path(path) if match_qpath(path, &paths::OPTION_NONE) => (false, true),
93         _ => (true, true),
94     }
95 }
96
97 struct ReturnVisitor<'a, 'tcx> {
98     cx: &'a LateContext<'tcx>,
99     arg_id: hir::HirId,
100     // Found a non-None return that isn't Some(input)
101     found_mapping: bool,
102     // Found a return that isn't Some
103     found_filtering: bool,
104 }
105
106 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
107     fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
108         ReturnVisitor {
109             cx,
110             arg_id,
111             found_mapping: false,
112             found_filtering: false,
113         }
114     }
115 }
116
117 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
118     type Map = Map<'tcx>;
119
120     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
121         if let hir::ExprKind::Ret(Some(expr)) = &expr.kind {
122             let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
123             self.found_mapping |= found_mapping;
124             self.found_filtering |= found_filtering;
125         } else {
126             walk_expr(self, expr);
127         }
128     }
129
130     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
131         NestedVisitorMap::None
132     }
133 }