1 use crate::utils::paths;
2 use crate::utils::usage::mutated_variables;
3 use crate::utils::{match_qpath, match_trait_method, span_lint};
5 use rustc::hir::def::Res;
6 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
7 use rustc::lint::LateContext;
9 use if_chain::if_chain;
11 use super::UNNECESSARY_FILTER_MAP;
13 pub(super) fn lint(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::Expr]) {
14 if !match_trait_method(cx, expr, &paths::ITERATOR) {
18 if let hir::ExprKind::Closure(_, _, body_id, ..) = args[1].node {
19 let body = cx.tcx.hir().body(body_id);
20 let arg_id = body.arguments[0].pat.hir_id;
21 let mutates_arg = match mutated_variables(&body.value, cx) {
22 Some(used_mutably) => used_mutably.contains(&arg_id),
26 let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
28 let mut return_visitor = ReturnVisitor::new(&cx, arg_id);
29 return_visitor.visit_expr(&body.value);
30 found_mapping |= return_visitor.found_mapping;
31 found_filtering |= return_visitor.found_filtering;
36 UNNECESSARY_FILTER_MAP,
38 "this `.filter_map` can be written more simply using `.map`",
43 if !found_mapping && !mutates_arg {
46 UNNECESSARY_FILTER_MAP,
48 "this `.filter_map` can be written more simply using `.filter`",
55 // returns (found_mapping, found_filtering)
56 fn check_expression<'a, 'tcx>(
57 cx: &'a LateContext<'a, 'tcx>,
59 expr: &'tcx hir::Expr,
62 hir::ExprKind::Call(ref func, ref args) => {
64 if let hir::ExprKind::Path(ref path) = func.node;
66 if match_qpath(path, &paths::OPTION_SOME) {
68 if let hir::ExprKind::Path(path) = &args[0].node;
69 if let Res::Local(ref local) = cx.tables.qpath_res(path, args[0].hir_id);
78 // We don't know. It might do anything.
85 hir::ExprKind::Block(ref block, _) => {
86 if let Some(expr) = &block.expr {
87 check_expression(cx, arg_id, &expr)
92 hir::ExprKind::Match(_, ref arms, _) => {
93 let mut found_mapping = false;
94 let mut found_filtering = false;
96 let (m, f) = check_expression(cx, arg_id, &arm.body);
100 (found_mapping, found_filtering)
102 hir::ExprKind::Path(path) if match_qpath(path, &paths::OPTION_NONE) => (false, true),
107 struct ReturnVisitor<'a, 'tcx> {
108 cx: &'a LateContext<'a, 'tcx>,
110 // Found a non-None return that isn't Some(input)
112 // Found a return that isn't Some
113 found_filtering: bool,
116 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
117 fn new(cx: &'a LateContext<'a, 'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
121 found_mapping: false,
122 found_filtering: false,
127 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
128 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
129 if let hir::ExprKind::Ret(Some(expr)) = &expr.node {
130 let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
131 self.found_mapping |= found_mapping;
132 self.found_filtering |= found_filtering;
134 walk_expr(self, expr);
138 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
139 NestedVisitorMap::None