3 use super::OBFUSCATED_IF_ELSE;
4 use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_with_applicability};
5 use rustc_errors::Applicability;
7 use rustc_lint::LateContext;
9 pub(super) fn check<'tcx>(
10 cx: &LateContext<'tcx>,
11 expr: &'tcx hir::Expr<'_>,
12 then_recv: &'tcx hir::Expr<'_>,
13 then_arg: &'tcx hir::Expr<'_>,
14 unwrap_arg: &'tcx hir::Expr<'_>,
16 // something.then_some(blah).unwrap_or(blah)
17 // ^^^^^^^^^-then_recv ^^^^-then_arg ^^^^- unwrap_arg
18 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- expr
20 let recv_ty = cx.typeck_results().expr_ty(then_recv);
22 if recv_ty.is_bool() {
23 let mut applicability = Applicability::MachineApplicable;
25 "if {} {{ {} }} else {{ {} }}",
26 snippet_with_applicability(cx, then_recv.span, "..", &mut applicability),
27 snippet_with_applicability(cx, then_arg.span, "..", &mut applicability),
28 snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability)
35 "use of `.then_some(..).unwrap_or(..)` can be written \
36 more clearly with `if .. else ..`",