|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 4 | +use clippy_utils::{expr_or_init, find_binding_init, peel_blocks}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::def::{DefKind, Res}; |
| 7 | +use rustc_hir::{Body, BodyId, Closure, Expr, ExprKind, HirId, QPath}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_span::symbol::sym; |
| 10 | + |
| 11 | +use super::UNNECESSARY_OPTION_MAP_OR_ELSE; |
| 12 | +use super::utils::get_last_chain_binding_hir_id; |
| 13 | + |
| 14 | +fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>) { |
| 15 | + let msg = "unused \"map closure\" when calling `Option::map_or_else` value"; |
| 16 | + let mut applicability = Applicability::MachineApplicable; |
| 17 | + let self_snippet = snippet_with_applicability(cx, recv.span, "_", &mut applicability); |
| 18 | + let err_snippet = snippet_with_applicability(cx, def_arg.span, "..", &mut applicability); |
| 19 | + span_lint_and_sugg( |
| 20 | + cx, |
| 21 | + UNNECESSARY_OPTION_MAP_OR_ELSE, |
| 22 | + expr.span, |
| 23 | + msg, |
| 24 | + "consider using `unwrap_or_else`", |
| 25 | + format!("{self_snippet}.unwrap_or_else({err_snippet})"), |
| 26 | + Applicability::MachineApplicable, |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +fn handle_qpath( |
| 31 | + cx: &LateContext<'_>, |
| 32 | + expr: &Expr<'_>, |
| 33 | + recv: &Expr<'_>, |
| 34 | + def_arg: &Expr<'_>, |
| 35 | + expected_hir_id: HirId, |
| 36 | + qpath: QPath<'_>, |
| 37 | +) { |
| 38 | + if let QPath::Resolved(_, path) = qpath |
| 39 | + && let Res::Local(hir_id) = path.res |
| 40 | + && expected_hir_id == hir_id |
| 41 | + { |
| 42 | + emit_lint(cx, expr, recv, def_arg); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn handle_closure(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>, body_id: BodyId) { |
| 47 | + let body = cx.tcx.hir_body(body_id); |
| 48 | + handle_fn_body(cx, expr, recv, def_arg, body); |
| 49 | +} |
| 50 | + |
| 51 | +fn handle_fn_body(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>, body: &Body<'_>) { |
| 52 | + if let Some(first_param) = body.params.first() { |
| 53 | + let body_expr = peel_blocks(body.value); |
| 54 | + match body_expr.kind { |
| 55 | + ExprKind::Path(qpath) => { |
| 56 | + handle_qpath(cx, expr, recv, def_arg, first_param.pat.hir_id, qpath); |
| 57 | + }, |
| 58 | + // If this is a block (that wasn't peeled off), then it means there are statements. |
| 59 | + ExprKind::Block(block, _) => { |
| 60 | + if let Some(block_expr) = block.expr |
| 61 | + // First we ensure that this is a "binding chain" (each statement is a binding |
| 62 | + // of the previous one) and that it is a binding of the closure argument. |
| 63 | + && let Some(last_chain_binding_id) = |
| 64 | + get_last_chain_binding_hir_id(first_param.pat.hir_id, block.stmts) |
| 65 | + && let ExprKind::Path(qpath) = block_expr.kind |
| 66 | + { |
| 67 | + handle_qpath(cx, expr, recv, def_arg, last_chain_binding_id, qpath); |
| 68 | + } |
| 69 | + }, |
| 70 | + _ => {}, |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// lint use of `_.map_or_else(|err| err, |n| n)` for `Option`s. |
| 76 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>, map_arg: &Expr<'_>) { |
| 77 | + // lint if the caller of `map_or_else()` is an `Option` |
| 78 | + if !is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) { |
| 79 | + return; |
| 80 | + } |
| 81 | + match map_arg.kind { |
| 82 | + // If the second argument is a closure, we can check its body. |
| 83 | + ExprKind::Closure(&Closure { body, .. }) => { |
| 84 | + handle_closure(cx, expr, recv, def_arg, body); |
| 85 | + }, |
| 86 | + ExprKind::Path(qpath) => { |
| 87 | + let res = cx.qpath_res(&qpath, map_arg.hir_id); |
| 88 | + match res { |
| 89 | + // Case 1: Local variable (could be a closure) |
| 90 | + Res::Local(hir_id) => { |
| 91 | + if let Some(init_expr) = find_binding_init(cx, hir_id) { |
| 92 | + let origin = expr_or_init(cx, init_expr); |
| 93 | + if let ExprKind::Closure(&Closure { body, .. }) = origin.kind { |
| 94 | + handle_closure(cx, expr, recv, def_arg, body); |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + // Case 2: Function definition |
| 99 | + Res::Def(DefKind::Fn, def_id) => { |
| 100 | + if let Some(local_def_id) = def_id.as_local() |
| 101 | + && let Some(body) = cx.tcx.hir_maybe_body_owned_by(local_def_id) |
| 102 | + { |
| 103 | + handle_fn_body(cx, expr, recv, def_arg, body); |
| 104 | + } |
| 105 | + }, |
| 106 | + _ => (), |
| 107 | + } |
| 108 | + }, |
| 109 | + _ => (), |
| 110 | + } |
| 111 | +} |
0 commit comments