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