]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/unnecessary_filter_map.rs
Auto merge of #7546 - mgeier:patch-1, r=giraffate
[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_lang_ctor, is_trait_method, path_to_local_id};
4 use rustc_hir as hir;
5 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
6 use rustc_hir::LangItem::{OptionNone, OptionSome};
7 use rustc_lint::LateContext;
8 use rustc_middle::hir::map::Map;
9 use rustc_middle::ty::{self, TyS};
10 use rustc_span::sym;
11
12 use super::UNNECESSARY_FILTER_MAP;
13
14 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
15     if !is_trait_method(cx, expr, sym::Iterator) {
16         return;
17     }
18
19     if let hir::ExprKind::Closure(_, _, body_id, ..) = arg.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         let sugg = if !found_filtering {
33             "map"
34         } else if !found_mapping && !mutates_arg {
35             let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
36             match cx.typeck_results().expr_ty(&body.value).kind() {
37                 ty::Adt(adt, subst)
38                     if cx.tcx.is_diagnostic_item(sym::option_type, adt.did)
39                         && TyS::same_type(in_ty, subst.type_at(0)) =>
40                 {
41                     "filter"
42                 },
43                 _ => return,
44             }
45         } else {
46             return;
47         };
48         span_lint(
49             cx,
50             UNNECESSARY_FILTER_MAP,
51             expr.span,
52             &format!("this `.filter_map` can be written more simply using `.{}`", sugg),
53         );
54     }
55 }
56
57 // returns (found_mapping, found_filtering)
58 fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
59     match &expr.kind {
60         hir::ExprKind::Call(func, args) => {
61             if let hir::ExprKind::Path(ref path) = func.kind {
62                 if is_lang_ctor(cx, path, OptionSome) {
63                     if path_to_local_id(&args[0], arg_id) {
64                         return (false, false);
65                     }
66                     return (true, false);
67                 }
68             }
69             (true, true)
70         },
71         hir::ExprKind::Block(block, _) => block
72             .expr
73             .as_ref()
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;
78             for arm in *arms {
79                 let (m, f) = check_expression(cx, arg_id, arm.body);
80                 found_mapping |= m;
81                 found_filtering |= f;
82             }
83             (found_mapping, found_filtering)
84         },
85         // There must be an else_arm or there will be a type error
86         hir::ExprKind::If(_, if_arm, Some(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)
90         },
91         hir::ExprKind::Path(path) if is_lang_ctor(cx, path, OptionNone) => (false, true),
92         _ => (true, true),
93     }
94 }
95
96 struct ReturnVisitor<'a, 'tcx> {
97     cx: &'a LateContext<'tcx>,
98     arg_id: hir::HirId,
99     // Found a non-None return that isn't Some(input)
100     found_mapping: bool,
101     // Found a return that isn't Some
102     found_filtering: bool,
103 }
104
105 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
106     fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
107         ReturnVisitor {
108             cx,
109             arg_id,
110             found_mapping: false,
111             found_filtering: false,
112         }
113     }
114 }
115
116 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
117     type Map = Map<'tcx>;
118
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;
124         } else {
125             walk_expr(self, expr);
126         }
127     }
128
129     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
130         NestedVisitorMap::None
131     }
132 }