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;
8 use rustc_middle::hir::map::Map;
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].kind {
20 let body = cx.tcx.hir().body(body_id);
21 let arg_id = body.params[0].pat.hir_id;
23 mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
25 let (mut found_mapping, mut found_filtering) = check_expression(&cx, arg_id, &body.value);
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;
35 UNNECESSARY_FILTER_MAP,
37 "this `.filter_map` can be written more simply using `.map`",
42 if !found_mapping && !mutates_arg {
45 UNNECESSARY_FILTER_MAP,
47 "this `.filter_map` can be written more simply using `.filter`",
54 // returns (found_mapping, found_filtering)
55 fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
57 hir::ExprKind::Call(ref func, ref args) => {
59 if let hir::ExprKind::Path(ref path) = func.kind;
61 if match_qpath(path, &paths::OPTION_SOME) {
63 if let hir::ExprKind::Path(path) = &args[0].kind;
64 if let Res::Local(ref local) = cx.qpath_res(path, args[0].hir_id);
73 // We don't know. It might do anything.
80 hir::ExprKind::Block(ref block, _) => block
83 .map_or((false, false), |expr| check_expression(cx, arg_id, &expr)),
84 hir::ExprKind::Match(_, arms, _) => {
85 let mut found_mapping = false;
86 let mut found_filtering = false;
88 let (m, f) = check_expression(cx, arg_id, &arm.body);
92 (found_mapping, found_filtering)
94 hir::ExprKind::Path(path) if match_qpath(path, &paths::OPTION_NONE) => (false, true),
99 struct ReturnVisitor<'a, 'tcx> {
100 cx: &'a LateContext<'tcx>,
102 // Found a non-None return that isn't Some(input)
104 // Found a return that isn't Some
105 found_filtering: bool,
108 impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
109 fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
113 found_mapping: false,
114 found_filtering: false,
119 impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
120 type Map = Map<'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;
128 walk_expr(self, expr);
132 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
133 NestedVisitorMap::None