|
| 1 | +use crate::utils::{ |
| 2 | + indent_of, is_type_diagnostic_item, match_qpath, paths, reindent_multiline, snippet_opt, span_lint_and_sugg, |
| 3 | +}; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir::{def, Expr, ExprKind, PatKind, QPath}; |
| 7 | +use rustc_lint::LintContext; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// **What it does:** |
| 14 | + /// Finds patterns that reimplement `Option::ok_or`. |
| 15 | + /// |
| 16 | + /// **Why is this bad?** |
| 17 | + /// Concise code helps focusing on behavior instead of boilerplate. |
| 18 | + /// |
| 19 | + /// **Known problems:** None. |
| 20 | + /// |
| 21 | + /// **Examples:** |
| 22 | + /// ```rust |
| 23 | + /// let foo: Option<i32> = None; |
| 24 | + /// foo.map_or(Err("error"), |v| Ok(v)); |
| 25 | + /// |
| 26 | + /// let foo: Option<i32> = None; |
| 27 | + /// foo.map_or(Err("error"), |v| Ok(v)); |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// let foo: Option<i32> = None; |
| 33 | + /// foo.ok_or("error"); |
| 34 | + /// ``` |
| 35 | + pub MANUAL_OK_OR, |
| 36 | + pedantic, |
| 37 | + "finds patterns that can be encoded more concisely with `Option::ok_or`" |
| 38 | +} |
| 39 | + |
| 40 | +declare_lint_pass!(ManualOkOr => [MANUAL_OK_OR]); |
| 41 | + |
| 42 | +impl LateLintPass<'_> for ManualOkOr { |
| 43 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, scrutinee: &'tcx Expr<'tcx>) { |
| 44 | + if in_external_macro(cx.sess(), scrutinee.span) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if_chain! { |
| 49 | + if let ExprKind::MethodCall(method_segment, _, args, _) = scrutinee.kind; |
| 50 | + if method_segment.ident.name == sym!(map_or); |
| 51 | + if args.len() == 3; |
| 52 | + let method_receiver = &args[0]; |
| 53 | + let ty = cx.typeck_results().expr_ty(method_receiver); |
| 54 | + if is_type_diagnostic_item(cx, ty, sym!(option_type)); |
| 55 | + let or_expr = &args[1]; |
| 56 | + if is_ok_wrapping(cx, &args[2]); |
| 57 | + if let ExprKind::Call(Expr { kind: ExprKind::Path(err_path), .. }, &[ref err_arg]) = or_expr.kind; |
| 58 | + if match_qpath(err_path, &paths::RESULT_ERR); |
| 59 | + if let Some(method_receiver_snippet) = snippet_opt(cx, method_receiver.span); |
| 60 | + if let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span); |
| 61 | + if let Some(indent) = indent_of(cx, scrutinee.span); |
| 62 | + then { |
| 63 | + let reindented_err_arg_snippet = |
| 64 | + reindent_multiline(err_arg_snippet.into(), true, Some(indent + 4)); |
| 65 | + span_lint_and_sugg( |
| 66 | + cx, |
| 67 | + MANUAL_OK_OR, |
| 68 | + scrutinee.span, |
| 69 | + "this pattern reimplements `Option::ok_or`", |
| 70 | + "replace with", |
| 71 | + format!( |
| 72 | + "{}.ok_or({})", |
| 73 | + method_receiver_snippet, |
| 74 | + reindented_err_arg_snippet |
| 75 | + ), |
| 76 | + Applicability::MachineApplicable, |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn is_ok_wrapping(cx: &LateContext<'_>, map_expr: &Expr<'_>) -> bool { |
| 84 | + if let ExprKind::Path(ref qpath) = map_expr.kind { |
| 85 | + if match_qpath(qpath, &paths::RESULT_OK) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + } |
| 89 | + if_chain! { |
| 90 | + if let ExprKind::Closure(_, _, body_id, ..) = map_expr.kind; |
| 91 | + let body = cx.tcx.hir().body(body_id); |
| 92 | + if let PatKind::Binding(_, param_id, ..) = body.params[0].pat.kind; |
| 93 | + if let ExprKind::Call(Expr { kind: ExprKind::Path(ok_path), .. }, &[ref ok_arg]) = body.value.kind; |
| 94 | + if match_qpath(ok_path, &paths::RESULT_OK); |
| 95 | + if let ExprKind::Path(QPath::Resolved(_, ok_arg_path)) = ok_arg.kind; |
| 96 | + if let def::Res::Local(ok_arg_path_id) = ok_arg_path.res; |
| 97 | + then { param_id == ok_arg_path_id } else { false } |
| 98 | + } |
| 99 | +} |
0 commit comments