summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs')
-rw-r--r--src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs82
1 files changed, 28 insertions, 54 deletions
diff --git a/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs b/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
index bafa6fc58..1cef6226a 100644
--- a/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
+++ b/src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs
@@ -2,9 +2,10 @@ use super::utils::clone_or_copy_needed;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::is_copy;
use clippy_utils::usage::mutated_variables;
-use clippy_utils::{is_lang_ctor, is_trait_method, path_to_local_id};
+use clippy_utils::visitors::{for_each_expr, Descend};
+use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
+use core::ops::ControlFlow;
use rustc_hir as hir;
-use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_hir::LangItem::{OptionNone, OptionSome};
use rustc_lint::LateContext;
use rustc_middle::ty;
@@ -13,7 +14,7 @@ use rustc_span::sym;
use super::UNNECESSARY_FILTER_MAP;
use super::UNNECESSARY_FIND_MAP;
-pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>, name: &str) {
+pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>, name: &str) {
if !is_trait_method(cx, expr, sym::Iterator) {
return;
}
@@ -21,22 +22,27 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<
if let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind {
let body = cx.tcx.hir().body(body);
let arg_id = body.params[0].pat.hir_id;
- let mutates_arg =
- mutated_variables(&body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
- let (clone_or_copy_needed, _) = clone_or_copy_needed(cx, body.params[0].pat, &body.value);
+ let mutates_arg = mutated_variables(body.value, cx).map_or(true, |used_mutably| used_mutably.contains(&arg_id));
+ let (clone_or_copy_needed, _) = clone_or_copy_needed(cx, body.params[0].pat, body.value);
- let (mut found_mapping, mut found_filtering) = check_expression(cx, arg_id, &body.value);
+ let (mut found_mapping, mut found_filtering) = check_expression(cx, arg_id, body.value);
- let mut return_visitor = ReturnVisitor::new(cx, arg_id);
- return_visitor.visit_expr(&body.value);
- found_mapping |= return_visitor.found_mapping;
- found_filtering |= return_visitor.found_filtering;
+ let _: Option<!> = for_each_expr(body.value, |e| {
+ if let hir::ExprKind::Ret(Some(e)) = &e.kind {
+ let (found_mapping_res, found_filtering_res) = check_expression(cx, arg_id, e);
+ found_mapping |= found_mapping_res;
+ found_filtering |= found_filtering_res;
+ ControlFlow::Continue(Descend::No)
+ } else {
+ ControlFlow::Continue(Descend::Yes)
+ }
+ });
let in_ty = cx.typeck_results().node_type(body.params[0].hir_id);
let sugg = if !found_filtering {
if name == "filter_map" { "map" } else { "map(..).next()" }
} else if !found_mapping && !mutates_arg && (!clone_or_copy_needed || is_copy(cx, in_ty)) {
- match cx.typeck_results().expr_ty(&body.value).kind() {
+ match cx.typeck_results().expr_ty(body.value).kind() {
ty::Adt(adt, subst)
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) && in_ty == subst.type_at(0) =>
{
@@ -55,22 +61,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<
UNNECESSARY_FIND_MAP
},
expr.span,
- &format!("this `.{}` can be written more simply using `.{}`", name, sugg),
+ &format!("this `.{name}` can be written more simply using `.{sugg}`"),
);
}
}
// returns (found_mapping, found_filtering)
fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tcx hir::Expr<'_>) -> (bool, bool) {
- match &expr.kind {
+ match expr.kind {
hir::ExprKind::Call(func, args) => {
- if let hir::ExprKind::Path(ref path) = func.kind {
- if is_lang_ctor(cx, path, OptionSome) {
- if path_to_local_id(&args[0], arg_id) {
- return (false, false);
- }
- return (true, false);
+ if is_res_lang_ctor(cx, path_res(cx, func), OptionSome) {
+ if path_to_local_id(&args[0], arg_id) {
+ return (false, false);
}
+ return (true, false);
}
(true, true)
},
@@ -81,7 +85,7 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
hir::ExprKind::Match(_, arms, _) => {
let mut found_mapping = false;
let mut found_filtering = false;
- for arm in *arms {
+ for arm in arms {
let (m, f) = check_expression(cx, arg_id, arm.body);
found_mapping |= m;
found_filtering |= f;
@@ -94,39 +98,9 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
let else_check = check_expression(cx, arg_id, else_arm);
(if_check.0 | else_check.0, if_check.1 | else_check.1)
},
- hir::ExprKind::Path(path) if is_lang_ctor(cx, path, OptionNone) => (false, true),
+ hir::ExprKind::Path(ref path) if is_res_lang_ctor(cx, cx.qpath_res(path, expr.hir_id), OptionNone) => {
+ (false, true)
+ },
_ => (true, true),
}
}
-
-struct ReturnVisitor<'a, 'tcx> {
- cx: &'a LateContext<'tcx>,
- arg_id: hir::HirId,
- // Found a non-None return that isn't Some(input)
- found_mapping: bool,
- // Found a return that isn't Some
- found_filtering: bool,
-}
-
-impl<'a, 'tcx> ReturnVisitor<'a, 'tcx> {
- fn new(cx: &'a LateContext<'tcx>, arg_id: hir::HirId) -> ReturnVisitor<'a, 'tcx> {
- ReturnVisitor {
- cx,
- arg_id,
- found_mapping: false,
- found_filtering: false,
- }
- }
-}
-
-impl<'a, 'tcx> Visitor<'tcx> for ReturnVisitor<'a, 'tcx> {
- fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
- if let hir::ExprKind::Ret(Some(expr)) = &expr.kind {
- let (found_mapping, found_filtering) = check_expression(self.cx, self.arg_id, expr);
- self.found_mapping |= found_mapping;
- self.found_filtering |= found_filtering;
- } else {
- walk_expr(self, expr);
- }
- }
-}