]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[rust.git] / src / tools / clippy / 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_lang_ctor, is_trait_method, path_to_local_id};
4 use rustc_hir as hir;
5 use rustc_hir::intravisit::{walk_expr, Visitor};
6 use rustc_hir::LangItem::{OptionNone, OptionSome};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty;
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         let sugg = if !found_filtering {
32             "map"
33         } else if !found_mapping && !mutates_arg {
34             let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
35             match cx.typeck_results().expr_ty(&body.value).kind() {
36                 ty::Adt(adt, subst) if cx.tcx.is_diagnostic_item(sym::Option, adt.did) && in_ty == subst.type_at(0) => {
37                     "filter"
38                 },
39                 _ => return,
40             }
41         } else {
42             return;
43         };
44         span_lint(
45             cx,
46             UNNECESSARY_FILTER_MAP,
47             expr.span,
48             &format!("this `.filter_map` can be written more simply using `.{}`", sugg),
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(func, args) => {
57             if let hir::ExprKind::Path(ref path) = func.kind {
58                 if is_lang_ctor(cx, path, OptionSome) {
59                     if path_to_local_id(&args[0], arg_id) {
60                         return (false, false);
61                     }
62                     return (true, false);
63                 }
64             }
65             (true, true)
66         },
67         hir::ExprKind::Block(block, _) => block
68             .expr
69             .as_ref()
70             .map_or((false, false), |expr| check_expression(cx, arg_id, expr)),
71         hir::ExprKind::Match(_, arms, _) => {
72             let mut found_mapping = false;
73             let mut found_filtering = false;
74             for arm in *arms {
75                 let (m, f) = check_expression(cx, arg_id, arm.body);
76                 found_mapping |= m;
77                 found_filtering |= f;
78             }
79             (found_mapping, found_filtering)
80         },
81         // There must be an else_arm or there will be a type error
82         hir::ExprKind::If(_, if_arm, Some(else_arm)) => {
83             let if_check = check_expression(cx, arg_id, if_arm);
84             let else_check = check_expression(cx, arg_id, else_arm);
85             (if_check.0 | else_check.0, if_check.1 | else_check.1)
86         },
87         hir::ExprKind::Path(path) if is_lang_ctor(cx, path, OptionNone) => (false, true),
88         _ => (true, true),
89     }
90 }
91
92 struct ReturnVisitor<'a, 'tcx> {
93     cx: &'a LateContext<'tcx>,
94     arg_id: hir::HirId,
95     // Found a non-None return that isn't Some(input)
96     found_mapping: bool,
97     // Found a return that isn't Some
98     found_filtering: bool,
99 }
100
101 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
102     fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
103         ReturnVisitor {
104             cx,
105             arg_id,
106             found_mapping: false,
107             found_filtering: false,
108         }
109     }
110 }
111
112 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
113     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
114         if let hir::ExprKind::Ret(Some(expr)) = &expr.kind {
115             let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
116             self.found_mapping |= found_mapping;
117             self.found_filtering |= found_filtering;
118         } else {
119             walk_expr(self, expr);
120         }
121     }
122 }