|
| 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 as hir; |
| 7 | +use rustc_hir::{Closure, Expr, ExprKind, HirId, QPath, Stmt, StmtKind}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_span::symbol::sym; |
| 10 | + |
| 11 | +use super::UNNECESSARY_RESULT_MAP_OR_ELSE; |
| 12 | + |
| 13 | +fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>) { |
| 14 | + let msg = "unused \"map closure\" when calling `Result::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_RESULT_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 get_last_chain_binding_hir_id(mut hir_id: HirId, statements: &[Stmt<'_>]) -> Option<HirId> { |
| 29 | + for stmt in statements { |
| 30 | + if let StmtKind::Local(local) = stmt.kind |
| 31 | + && let Some(init) = local.init |
| 32 | + && let ExprKind::Path(QPath::Resolved(_, path)) = init.kind |
| 33 | + && let hir::def::Res::Local(local_hir_id) = path.res |
| 34 | + && local_hir_id == hir_id |
| 35 | + { |
| 36 | + hir_id = local.pat.hir_id; |
| 37 | + } else { |
| 38 | + return None; |
| 39 | + } |
| 40 | + } |
| 41 | + Some(hir_id) |
| 42 | +} |
| 43 | + |
| 44 | +fn handle_qpath( |
| 45 | + cx: &LateContext<'_>, |
| 46 | + expr: &Expr<'_>, |
| 47 | + recv: &Expr<'_>, |
| 48 | + def_arg: &Expr<'_>, |
| 49 | + expected_hir_id: HirId, |
| 50 | + qpath: QPath<'_>, |
| 51 | +) { |
| 52 | + if let QPath::Resolved(_, path) = qpath |
| 53 | + && let hir::def::Res::Local(hir_id) = path.res |
| 54 | + && expected_hir_id == hir_id |
| 55 | + { |
| 56 | + emit_lint(cx, expr, recv, def_arg); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// lint use of `_.map_or_else(|err| err, |n| n)` for `Result`s. |
| 61 | +pub(super) fn check<'tcx>( |
| 62 | + cx: &LateContext<'tcx>, |
| 63 | + expr: &'tcx Expr<'_>, |
| 64 | + recv: &'tcx Expr<'_>, |
| 65 | + def_arg: &'tcx Expr<'_>, |
| 66 | + map_arg: &'tcx Expr<'_>, |
| 67 | +) { |
| 68 | + // lint if the caller of `map_or_else()` is a `Result` |
| 69 | + if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result) |
| 70 | + && let ExprKind::Closure(&Closure { body, .. }) = map_arg.kind |
| 71 | + && let body = cx.tcx.hir().body(body) |
| 72 | + && let Some(first_param) = body.params.first() |
| 73 | + { |
| 74 | + let body_expr = peel_blocks(body.value); |
| 75 | + |
| 76 | + match body_expr.kind { |
| 77 | + ExprKind::Path(qpath) => { |
| 78 | + handle_qpath(cx, expr, recv, def_arg, first_param.pat.hir_id, qpath); |
| 79 | + }, |
| 80 | + // If this is a block (that wasn't peeled off), then it means there are statements. |
| 81 | + ExprKind::Block(block, _) => { |
| 82 | + if let Some(block_expr) = block.expr |
| 83 | + // First we ensure that this is a "binding chain" (each statement is a binding |
| 84 | + // of the previous one) and that it is a binding of the closure argument. |
| 85 | + && let Some(last_chain_binding_id) = |
| 86 | + get_last_chain_binding_hir_id(first_param.pat.hir_id, block.stmts) |
| 87 | + && let ExprKind::Path(qpath) = block_expr.kind |
| 88 | + { |
| 89 | + handle_qpath(cx, expr, recv, def_arg, last_chain_binding_id, qpath); |
| 90 | + } |
| 91 | + }, |
| 92 | + _ => {}, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments