|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet; |
| 3 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 4 | +use clippy_utils::{is_res_lang_ctor, path_res}; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_hir as hir; |
| 7 | +use rustc_hir::LangItem::OptionSome; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_span::symbol::sym; |
| 10 | + |
| 11 | +use super::RESULT_MAP_OR_INTO_OPTION; |
| 12 | + |
| 13 | +/// lint use of `_.map_or(None, _)` for `Option`s and `Result`s |
| 14 | +pub(super) fn check<'tcx>( |
| 15 | + cx: &LateContext<'tcx>, |
| 16 | + expr: &'tcx hir::Expr<'_>, |
| 17 | + recv: &'tcx hir::Expr<'_>, |
| 18 | + map_arg: &'tcx hir::Expr<'_>, |
| 19 | +) { |
| 20 | + let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result); |
| 21 | + |
| 22 | + if !is_result { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + let f_arg_is_some = is_res_lang_ctor(cx, path_res(cx, map_arg), OptionSome); |
| 27 | + |
| 28 | + if f_arg_is_some { |
| 29 | + // if let hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }) = map_arg.kind {} |
| 30 | + let msg = "called `map_or_else(|_| None, Some)` on a `Result` value"; |
| 31 | + let self_snippet = snippet(cx, recv.span, ".."); |
| 32 | + span_lint_and_sugg( |
| 33 | + cx, |
| 34 | + RESULT_MAP_OR_INTO_OPTION, |
| 35 | + expr.span, |
| 36 | + msg, |
| 37 | + "try using `ok` instead", |
| 38 | + format!("{self_snippet}.ok()"), |
| 39 | + Applicability::MachineApplicable, |
| 40 | + ); |
| 41 | + } |
| 42 | +} |
0 commit comments