]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
Auto merge of #102692 - nnethercote:TokenStreamBuilder, r=Aaron1011
[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::visitors::{for_each_expr, Descend};
6 use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
7 use core::ops::ControlFlow;
8 use rustc_hir as hir;
9 use rustc_hir::LangItem::{OptionNone, OptionSome};
10 use rustc_lint::LateContext;
11 use rustc_middle::ty;
12 use rustc_span::sym;
13
14 use super::UNNECESSARY_FILTER_MAP;
15 use super::UNNECESSARY_FIND_MAP;
16
17 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>, name: &str) {
18     if !is_trait_method(cx, expr, sym::Iterator) {
19         return;
20     }
21
22     if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
23         let body = cx.tcx.hir().body(body);
24         let arg_id = body.params[0].pat.hir_id;
25         let mutates_arg = mutated_variables(body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
26         let (clone_or_copy_needed, _) = clone_or_copy_needed(cx, body.params[0].pat, body.value);
27
28         let (mut found_mapping, mut found_filtering) = check_expression(cx, arg_id, body.value);
29
30         let _: Option<!> = for_each_expr(body.value, |e| {
31             if let hir::ExprKind::Ret(Some(e)) = &e.kind {
32                 let (found_mapping_res, found_filtering_res) = check_expression(cx, arg_id, e);
33                 found_mapping |= found_mapping_res;
34                 found_filtering |= found_filtering_res;
35                 ControlFlow::Continue(Descend::No)
36             } else {
37                 ControlFlow::Continue(Descend::Yes)
38             }
39         });
40
41         let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
42         let sugg = if !found_filtering {
43             if name == "filter_map" { "map" } else { "map(..).next()" }
44         } else if !found_mapping && !mutates_arg && (!clone_or_copy_needed || is_copy(cx, in_ty)) {
45             match cx.typeck_results().expr_ty(body.value).kind() {
46                 ty::Adt(adt, subst)
47                     if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && in_ty == subst.type_at(0) =>
48                 {
49                     if name == "filter_map" { "filter" } else { "find" }
50                 },
51                 _ => return,
52             }
53         } else {
54             return;
55         };
56         span_lint(
57             cx,
58             if name == "filter_map" {
59                 UNNECESSARY_FILTER_MAP
60             } else {
61                 UNNECESSARY_FIND_MAP
62             },
63             expr.span,
64             &format!("this `.{name}` can be written more simply using `.{sugg}`"),
65         );
66     }
67 }
68
69 // returns (found_mapping, found_filtering)
70 fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
71     match expr.kind {
72         hir::ExprKind::Call(func, args) => {
73             if is_res_lang_ctor(cx, path_res(cx, func), OptionSome) {
74                 if path_to_local_id(&args[0], arg_id) {
75                     return (false, false);
76                 }
77                 return (true, false);
78             }
79             (true, true)
80         },
81         hir::ExprKind::Block(block, _) => block
82             .expr
83             .as_ref()
84             .map_or((false, false), |expr| check_expression(cx, arg_id, expr)),
85         hir::ExprKind::Match(_, arms, _) => {
86             let mut found_mapping = false;
87             let mut found_filtering = false;
88             for arm in arms {
89                 let (m, f) = check_expression(cx, arg_id, arm.body);
90                 found_mapping |= m;
91                 found_filtering |= f;
92             }
93             (found_mapping, found_filtering)
94         },
95         // There must be an else_arm or there will be a type error
96         hir::ExprKind::If(_, if_arm, Some(else_arm)) => {
97             let if_check = check_expression(cx, arg_id, if_arm);
98             let else_check = check_expression(cx, arg_id, else_arm);
99             (if_check.0 | else_check.0, if_check.1 | else_check.1)
100         },
101         hir::ExprKind::Path(ref path) if is_res_lang_ctor(cx, cx.qpath_res(path, expr.hir_id), OptionNone) => {
102             (false, true)
103         },
104         _ => (true, true),
105     }
106 }