|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::peel_blocks; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{Closure, Expr, ExprKind, HirId, QPath}; |
| 7 | +use rustc_lint::LateContext; |
| 8 | +use rustc_span::symbol::sym; |
| 9 | + |
| 10 | +use super::UNNECESSARY_OPTION_MAP_OR_ELSE; |
| 11 | +use super::utils::get_last_chain_binding_hir_id; |
| 12 | + |
| 13 | +fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>) { |
| 14 | + let msg = "unused \"map closure\" when calling `Option::map_or_else` value"; |
| 15 | + let self_snippet = snippet(cx, recv.span, ".."); |
| 16 | + let err_snippet = snippet(cx, def_arg.span, ".."); |
| 17 | + span_lint_and_sugg( |
| 18 | + cx, |
| 19 | + UNNECESSARY_OPTION_MAP_OR_ELSE, |
| 20 | + expr.span, |
| 21 | + msg, |
| 22 | + "consider using `unwrap_or_else`", |
| 23 | + format!("{self_snippet}.unwrap_or_else({err_snippet})"), |
| 24 | + Applicability::MachineApplicable, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +fn handle_qpath( |
| 29 | + cx: &LateContext<'_>, |
| 30 | + expr: &Expr<'_>, |
| 31 | + recv: &Expr<'_>, |
| 32 | + def_arg: &Expr<'_>, |
| 33 | + expected_hir_id: HirId, |
| 34 | + qpath: QPath<'_>, |
| 35 | +) { |
| 36 | + if let QPath::Resolved(_, path) = qpath |
| 37 | + && let rustc_hir::def::Res::Local(hir_id) = path.res |
| 38 | + && expected_hir_id == hir_id |
| 39 | + { |
| 40 | + emit_lint(cx, expr, recv, def_arg); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// lint use of `_.map_or_else(|err| err, |n| n)` for `Option`s. |
| 45 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>, map_arg: &Expr<'_>) { |
| 46 | + // lint if the caller of `map_or_else()` is an `Option` |
| 47 | + if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) |
| 48 | + && let ExprKind::Closure(&Closure { body, .. }) = map_arg.kind |
| 49 | + && let body = cx.tcx.hir_body(body) |
| 50 | + && let Some(first_param) = body.params.first() |
| 51 | + { |
| 52 | + let body_expr = peel_blocks(body.value); |
| 53 | + |
| 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 | +} |
0 commit comments