1 use clippy_utils::source::snippet_with_applicability;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::{diagnostics::span_lint_and_sugg, is_res_lang_ctor, path_res};
4 use rustc_errors::Applicability;
5 use rustc_hir::{lang_items::LangItem, Expr, ExprKind};
6 use rustc_lint::LateContext;
7 use rustc_span::{sym, Span};
9 use super::OR_THEN_UNWRAP;
11 pub(super) fn check<'tcx>(
12 cx: &LateContext<'tcx>,
13 unwrap_expr: &Expr<'_>,
14 recv: &'tcx Expr<'tcx>,
15 or_arg: &'tcx Expr<'_>,
18 let ty = cx.typeck_results().expr_ty(recv); // get type of x (we later check if it's Option or Result)
20 let or_arg_content: Span;
22 if is_type_diagnostic_item(cx, ty, sym::Option) {
23 title = "found `.or(Some(…)).unwrap()`";
24 if let Some(content) = get_content_if_ctor_matches(cx, or_arg, LangItem::OptionSome) {
25 or_arg_content = content;
29 } else if is_type_diagnostic_item(cx, ty, sym::Result) {
30 title = "found `.or(Ok(…)).unwrap()`";
31 if let Some(content) = get_content_if_ctor_matches(cx, or_arg, LangItem::ResultOk) {
32 or_arg_content = content;
37 // Someone has implemented a struct with .or(...).unwrap() chaining,
38 // but it's not an Option or a Result, so bail
42 let mut applicability = Applicability::MachineApplicable;
43 let suggestion = format!(
45 snippet_with_applicability(cx, or_arg_content, "..", &mut applicability)
51 unwrap_expr.span.with_lo(or_span.lo()),
59 fn get_content_if_ctor_matches(cx: &LateContext<'_>, expr: &Expr<'_>, item: LangItem) -> Option<Span> {
60 if let ExprKind::Call(some_expr, [arg]) = expr.kind
61 && is_res_lang_ctor(cx, path_res(cx, some_expr), item)