2 use crate::rustc::hir::def::Def;
3 use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
4 use crate::rustc::lint::LateContext;
5 use crate::syntax::ast;
6 use crate::utils::paths;
7 use crate::utils::usage::mutated_variables;
8 use crate::utils::{match_qpath, match_trait_method, span_lint};
10 use if_chain::if_chain;
12 use super::UNNECESSARY_FILTER_MAP;
14 pub(super) fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::Expr]) {
15 if !match_trait_method(cx, expr, &paths::ITERATOR) {
19 if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].node {
20 let body = cx.tcx.hir.body(body_id);
21 let arg_id = body.arguments[0].pat.id;
22 let mutates_arg = match mutated_variables(&body.value, cx) {
23 Some(used_mutably) => used_mutably.contains(&arg_id),
27 let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
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;
37 UNNECESSARY_FILTER_MAP,
39 "this `.filter_map` can be written more simply using `.map`",
44 if !found_mapping && !mutates_arg {
47 UNNECESSARY_FILTER_MAP,
49 "this `.filter_map` can be written more simply using `.filter`",
56 // returns (found_mapping, found_filtering)
57 fn check_expression<'a, 'tcx: 'a>(
58 cx: &'a LateContext<'a, 'tcx>,
60 expr: &'tcx hir::Expr,
63 hir::ExprKind::Call(ref func, ref args) => {
65 if let hir::ExprKind::Path(ref path) = func.node;
67 if match_qpath(path, &paths::OPTION_SOME) {
69 if let hir::ExprKind::Path(path) = &args[0].node;
70 if let Def::Local(ref local) = cx.tables.qpath_def(path, args[0].hir_id);
79 // We don't know. It might do anything.
86 hir::ExprKind::Block(ref block, _) => {
87 if let Some(expr) = &block.expr {
88 check_expression(cx, arg_id, &expr)
93 // There must be an else_arm or there will be a type error
94 hir::ExprKind::If(_, ref if_arm, Some(ref else_arm)) => {
95 let if_check = check_expression(cx, arg_id, if_arm);
96 let else_check = check_expression(cx, arg_id, else_arm);
97 (if_check.0 | else_check.0, if_check.1 | else_check.1)
99 hir::ExprKind::Match(_, ref arms, _) => {
100 let mut found_mapping = false;
101 let mut found_filtering = false;
103 let (m, f) = check_expression(cx, arg_id, &arm.body);
105 found_filtering |= f;
107 (found_mapping, found_filtering)
109 hir::ExprKind::Path(path) if match_qpath(path, &paths::OPTION_NONE) => (false, true),
114 struct ReturnVisitor<'a, 'tcx: 'a> {
115 cx: &'a LateContext<'a, 'tcx>,
117 // Found a non-None return that isn't Some(input)
119 // Found a return that isn't Some
120 found_filtering: bool,
123 impl<'a, 'tcx: 'a> ReturnVisitor<'a, 'tcx> {
124 fn new(cx: &'a LateContext<'a, 'tcx>, arg_id: ast::NodeId) -> ReturnVisitor<'a, 'tcx> {
128 found_mapping: false,
129 found_filtering: false,
134 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
135 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
136 if let hir::ExprKind::Ret(Some(expr)) = &expr.node {
137 let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
138 self.found_mapping |= found_mapping;
139 self.found_filtering |= found_filtering;
141 walk_expr(self, expr);
145 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
146 NestedVisitorMap::None