|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use clippy_utils::{is_res_lang_ctor, path_res, peel_blocks}; |
| 4 | +use rustc_ast::BindingMode; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk}; |
| 7 | +use rustc_hir::def::{DefKind, Res}; |
| 8 | +use rustc_hir::{Arm, Expr, ExprKind, Pat, PatKind, Path, QPath}; |
| 9 | +use rustc_lint::{LateContext, LintContext}; |
| 10 | +use rustc_span::symbol::Ident; |
| 11 | + |
| 12 | +use super::MANUAL_OK_ERR; |
| 13 | + |
| 14 | +pub(crate) fn check_if_let( |
| 15 | + cx: &LateContext<'_>, |
| 16 | + expr: &Expr<'_>, |
| 17 | + let_pat: &Pat<'_>, |
| 18 | + let_expr: &Expr<'_>, |
| 19 | + if_then: &Expr<'_>, |
| 20 | + else_expr: &Expr<'_>, |
| 21 | +) { |
| 22 | + if let Some((is_ok, ident)) = is_ok_or_err(cx, let_pat) |
| 23 | + && is_some_ident(cx, if_then, ident) |
| 24 | + && is_none(cx, else_expr) |
| 25 | + { |
| 26 | + apply_lint(cx, expr, let_expr, is_ok); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +pub(crate) fn check_match(cx: &LateContext<'_>, expr: &Expr<'_>, scrutinee: &Expr<'_>, arms: &[Arm<'_>]) { |
| 31 | + if arms.len() == 2 |
| 32 | + && arms.iter().all(|arm| arm.guard.is_none()) |
| 33 | + && let Some((idx, is_ok)) = arms.iter().enumerate().find_map(|(arm_idx, arm)| { |
| 34 | + if let Some((is_ok, ident)) = is_ok_or_err(cx, arm.pat) |
| 35 | + && is_some_ident(cx, arm.body, ident) |
| 36 | + { |
| 37 | + Some((arm_idx, is_ok)) |
| 38 | + } else { |
| 39 | + None |
| 40 | + } |
| 41 | + }) |
| 42 | + && is_none(cx, arms[1 - idx].body) |
| 43 | + { |
| 44 | + apply_lint(cx, expr, scrutinee, is_ok); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Return `Some((true, IDENT))` if `pat` contains `Ok(IDENT)`, `Some((false, IDENT))` if it |
| 49 | +/// contains `Err(IDENT)`, `None` otherwise. |
| 50 | +fn is_ok_or_err<'hir>(cx: &LateContext<'_>, pat: &Pat<'hir>) -> Option<(bool, &'hir Ident)> { |
| 51 | + if let PatKind::TupleStruct(qpath, [arg], _) = &pat.kind |
| 52 | + && let PatKind::Binding(BindingMode::NONE, _, ident, _) = &arg.kind |
| 53 | + && pat.default_binding_modes |
| 54 | + && let res = cx.qpath_res(qpath, pat.hir_id) |
| 55 | + && let Res::Def(DefKind::Ctor(..), id) = res |
| 56 | + && let id @ Some(_) = cx.tcx.opt_parent(id) |
| 57 | + { |
| 58 | + if id == cx.tcx.lang_items().get(ResultOk) { |
| 59 | + return Some((true, ident)); |
| 60 | + } else if id == cx.tcx.lang_items().get(ResultErr) { |
| 61 | + return Some((false, ident)); |
| 62 | + } |
| 63 | + } |
| 64 | + None |
| 65 | +} |
| 66 | + |
| 67 | +/// Check if `expr` contains `Some(ident)`, possibly as a block |
| 68 | +fn is_some_ident(cx: &LateContext<'_>, expr: &Expr<'_>, ident: &Ident) -> bool { |
| 69 | + if let ExprKind::Call(body_callee, [body_arg]) = peel_blocks(expr).kind |
| 70 | + && is_res_lang_ctor(cx, path_res(cx, body_callee), OptionSome) |
| 71 | + && let ExprKind::Path(QPath::Resolved( |
| 72 | + _, |
| 73 | + Path { |
| 74 | + segments: [segment], .. |
| 75 | + }, |
| 76 | + )) = body_arg.kind |
| 77 | + { |
| 78 | + segment.ident.name == ident.name |
| 79 | + } else { |
| 80 | + false |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Check if `expr` is `None`, possibly as a block |
| 85 | +fn is_none(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { |
| 86 | + is_res_lang_ctor(cx, path_res(cx, peel_blocks(expr)), OptionNone) |
| 87 | +} |
| 88 | + |
| 89 | +/// Suggest replacing `expr` by `scrutinee.METHOD()`, where `METHOD` is either `ok` or |
| 90 | +/// `err`, depending on `is_ok`. |
| 91 | +fn apply_lint(cx: &LateContext<'_>, expr: &Expr<'_>, scrutinee: &Expr<'_>, is_ok: bool) { |
| 92 | + let method = if is_ok { "ok" } else { "err" }; |
| 93 | + let mut app = Applicability::MachineApplicable; |
| 94 | + let snippet = snippet_with_applicability(cx.sess(), scrutinee.span, "..", &mut app); |
| 95 | + span_lint_and_sugg( |
| 96 | + cx, |
| 97 | + MANUAL_OK_ERR, |
| 98 | + expr.span, |
| 99 | + format!("manual implementation of `{method}`"), |
| 100 | + "replace with", |
| 101 | + format!("{snippet}.{method}()"), |
| 102 | + app, |
| 103 | + ); |
| 104 | +} |
0 commit comments