1 use clippy_utils
::diagnostics
::span_lint_and_then
;
2 use clippy_utils
::is_unit_expr
;
3 use clippy_utils
::source
::{expr_block, snippet}
;
4 use clippy_utils
::sugg
::Sugg
;
5 use rustc_ast
::LitKind
;
6 use rustc_errors
::Applicability
;
7 use rustc_hir
::{Arm, Expr, ExprKind, PatKind}
;
8 use rustc_lint
::LateContext
;
11 use super::MATCH_BOOL
;
13 pub(crate) fn check(cx
: &LateContext
<'_
>, ex
: &Expr
<'_
>, arms
: &[Arm
<'_
>], expr
: &Expr
<'_
>) {
14 // Type of expression is `bool`.
15 if *cx
.typeck_results().expr_ty(ex
).kind() == ty
::Bool
{
20 "you seem to be trying to match on a boolean expression",
24 let exprs
= if let PatKind
::Lit(arm_bool
) = arms
[0].pat
.kind
{
25 if let ExprKind
::Lit(ref lit
) = arm_bool
.kind
{
27 LitKind
::Bool(true) => Some((arms
[0].body
, arms
[1].body
)),
28 LitKind
::Bool(false) => Some((arms
[1].body
, arms
[0].body
)),
38 if let Some((true_expr
, false_expr
)) = exprs
{
39 let sugg
= match (is_unit_expr(true_expr
), is_unit_expr(false_expr
)) {
40 (false, false) => Some(format
!(
42 snippet(cx
, ex
.span
, "b"),
43 expr_block(cx
, true_expr
, None
, "..", Some(expr
.span
)),
44 expr_block(cx
, false_expr
, None
, "..", Some(expr
.span
))
46 (false, true) => Some(format
!(
48 snippet(cx
, ex
.span
, "b"),
49 expr_block(cx
, true_expr
, None
, "..", Some(expr
.span
))
52 let test
= Sugg
::hir(cx
, ex
, "..");
56 expr_block(cx
, false_expr
, None
, "..", Some(expr
.span
))
62 if let Some(sugg
) = sugg
{
65 "consider using an `if`/`else` expression",
67 Applicability
::HasPlaceholders
,