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