|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::msrvs::{self, Msrv}; |
| 3 | +use clippy_utils::peel_blocks; |
| 4 | +use clippy_utils::res::{MaybeDef, MaybeResPath}; |
| 5 | +use clippy_utils::source::snippet_with_applicability; |
| 6 | +use clippy_utils::visitors::for_each_expr_without_closures; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{self as hir, Expr, ExprKind, HirId, PatKind}; |
| 9 | +use rustc_lint::LateContext; |
| 10 | +use rustc_span::symbol::sym; |
| 11 | +use std::ops::ControlFlow; |
| 12 | + |
| 13 | +use super::MANUAL_OPTION_ZIP; |
| 14 | + |
| 15 | +/// Checks for `a.and_then(|a| b.map(|b| (a, b)))` and suggests `a.zip(b)`. |
| 16 | +pub(super) fn check<'tcx>( |
| 17 | + cx: &LateContext<'tcx>, |
| 18 | + expr: &'tcx Expr<'tcx>, |
| 19 | + recv: &'tcx Expr<'_>, |
| 20 | + arg: &'tcx Expr<'_>, |
| 21 | + msrv: Msrv, |
| 22 | +) { |
| 23 | + // Looking for: `a.and_then(|a| b.map(|b| (a, b)))`. |
| 24 | + // `and_then(|a| ...)` |
| 25 | + if let ExprKind::Closure(&hir::Closure { body: outer_body_id, .. }) = arg.kind |
| 26 | + && let hir::Body { params: [outer_param], value: outer_value, .. } = cx.tcx.hir_body(outer_body_id) |
| 27 | + && let PatKind::Binding(_, outer_param_id, _, None) = outer_param.pat.kind |
| 28 | + && cx.typeck_results().expr_ty(recv).is_diag_item(cx, sym::Option) |
| 29 | + // `b.map(|b| ...)` |
| 30 | + && let ExprKind::MethodCall(method_path, map_recv, [map_arg], _) = peel_blocks(outer_value).kind |
| 31 | + && method_path.ident.name == sym::map |
| 32 | + && cx.typeck_results().expr_ty(map_recv).is_diag_item(cx, sym::Option) |
| 33 | + // `b` does not reference the outer closure parameter `a`. |
| 34 | + && for_each_expr_without_closures(map_recv, |e| { |
| 35 | + if e.res_local_id() == Some(outer_param_id) { |
| 36 | + ControlFlow::Break(()) |
| 37 | + } else { |
| 38 | + ControlFlow::Continue(()) |
| 39 | + } |
| 40 | + }).is_none() |
| 41 | + // `|b| (a, b)` |
| 42 | + && let ExprKind::Closure(&hir::Closure { body: inner_body_id, .. }) = map_arg.kind |
| 43 | + && let hir::Body { params: [inner_param], value: inner_value, .. } = cx.tcx.hir_body(inner_body_id) |
| 44 | + && let PatKind::Binding(_, inner_param_id, _, None) = inner_param.pat.kind |
| 45 | + // `(a, b)` or `(b, a)` — tuple of outer and inner param in either order. |
| 46 | + && let ExprKind::Tup([first, second]) = peel_blocks(inner_value).kind |
| 47 | + && let Some((zip_recv, zip_arg)) = zip_operands(first, second, outer_param_id, inner_param_id, recv, map_recv) |
| 48 | + // `Option.zip()` is available. |
| 49 | + && msrv.meets(cx, msrvs::OPTION_ZIP) |
| 50 | + { |
| 51 | + let mut applicability = Applicability::MachineApplicable; |
| 52 | + let zip_recv_snip = snippet_with_applicability(cx, zip_recv.span, "_", &mut applicability); |
| 53 | + let zip_arg_snip = snippet_with_applicability(cx, zip_arg.span, "_", &mut applicability); |
| 54 | + let suggestion = format!("{zip_recv_snip}.zip({zip_arg_snip})"); |
| 55 | + |
| 56 | + span_lint_and_sugg( |
| 57 | + cx, |
| 58 | + MANUAL_OPTION_ZIP, |
| 59 | + expr.span, |
| 60 | + "manual implementation of `Option::zip`", |
| 61 | + "use", |
| 62 | + suggestion, |
| 63 | + applicability, |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Given the two tuple elements and the `and_then` receiver / `map` receiver, returns the |
| 69 | +/// `(zip_receiver, zip_argument)` expressions for the `.zip()` suggestion. |
| 70 | +/// |
| 71 | +/// For `(outer, inner)` order the zip is `recv.zip(map_recv)`. |
| 72 | +/// For `(inner, outer)` (reversed) the zip is `map_recv.zip(recv)`. |
| 73 | +/// Returns `None` if the tuple elements don't match either order. |
| 74 | +fn zip_operands<'a>( |
| 75 | + first: &Expr<'_>, |
| 76 | + second: &Expr<'_>, |
| 77 | + outer_param_id: HirId, |
| 78 | + inner_param_id: HirId, |
| 79 | + recv: &'a Expr<'a>, |
| 80 | + map_recv: &'a Expr<'a>, |
| 81 | +) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> { |
| 82 | + if first.res_local_id() == Some(outer_param_id) && second.res_local_id() == Some(inner_param_id) { |
| 83 | + Some((recv, map_recv)) |
| 84 | + } else if first.res_local_id() == Some(inner_param_id) && second.res_local_id() == Some(outer_param_id) { |
| 85 | + Some((map_recv, recv)) |
| 86 | + } else { |
| 87 | + None |
| 88 | + } |
| 89 | +} |
0 commit comments