Skip to content

Commit 22b2200

Browse files
committed
Simplify needless_bool
1 parent 47c0885 commit 22b2200

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
//! This lint is **warn** by default
44
55
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
6-
use clippy_utils::higher;
76
use clippy_utils::source::snippet_with_applicability;
87
use clippy_utils::sugg::Sugg;
9-
use clippy_utils::{is_else_clause, is_expn_of};
8+
use clippy_utils::{higher, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt};
109
use rustc_ast::ast::LitKind;
1110
use rustc_errors::Applicability;
12-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
11+
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
1312
use rustc_lint::{LateContext, LateLintPass};
1413
use rustc_session::{declare_lint_pass, declare_tool_lint};
1514
use rustc_span::source_map::Spanned;
@@ -109,8 +108,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
109108
applicability,
110109
);
111110
};
112-
if let ExprKind::Block(then, _) = then.kind {
113-
match (fetch_bool_block(then), fetch_bool_expr(r#else)) {
111+
if let Some((a, b)) = fetch_bool_block(then).and_then(|a| Some((a, fetch_bool_block(r#else)?))) {
112+
match (a, b) {
114113
(RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
115114
span_lint(
116115
cx,
@@ -133,8 +132,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
133132
(Bool(false), Bool(true)) => reduce(false, true),
134133
_ => (),
135134
}
136-
} else {
137-
panic!("IfExpr `then` node is not an `ExprKind::Block`");
138135
}
139136
}
140137
}
@@ -237,8 +234,6 @@ fn check_comparison<'a, 'tcx>(
237234
right_false: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
238235
no_literal: Option<(impl FnOnce(Sugg<'a>, Sugg<'a>) -> Sugg<'a>, &str)>,
239236
) {
240-
use self::Expression::{Bool, Other};
241-
242237
if let ExprKind::Binary(op, left_side, right_side) = e.kind {
243238
let (l_ty, r_ty) = (
244239
cx.typeck_results().expr_ty(left_side),
@@ -270,19 +265,19 @@ fn check_comparison<'a, 'tcx>(
270265
}
271266

272267
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
273-
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
268+
(Some(true), None) => left_true.map_or((), |(h, m)| {
274269
suggest_bool_comparison(cx, e, right_side, applicability, m, h);
275270
}),
276-
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
271+
(None, Some(true)) => right_true.map_or((), |(h, m)| {
277272
suggest_bool_comparison(cx, e, left_side, applicability, m, h);
278273
}),
279-
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
274+
(Some(false), None) => left_false.map_or((), |(h, m)| {
280275
suggest_bool_comparison(cx, e, right_side, applicability, m, h);
281276
}),
282-
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
277+
(None, Some(false)) => right_false.map_or((), |(h, m)| {
283278
suggest_bool_comparison(cx, e, left_side, applicability, m, h);
284279
}),
285-
(Other, Other) => no_literal.map_or((), |(h, m)| {
280+
(None, None) => no_literal.map_or((), |(h, m)| {
286281
let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
287282
let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
288283
span_lint_and_sugg(
@@ -331,41 +326,20 @@ fn suggest_bool_comparison<'a, 'tcx>(
331326
enum Expression {
332327
Bool(bool),
333328
RetBool(bool),
334-
Other,
335329
}
336330

337-
fn fetch_bool_block(block: &Block<'_>) -> Expression {
338-
match (&*block.stmts, block.expr.as_ref()) {
339-
(&[], Some(e)) => fetch_bool_expr(&**e),
340-
(&[ref e], None) => {
341-
if let StmtKind::Semi(e) = e.kind {
342-
if let ExprKind::Ret(_) = e.kind {
343-
fetch_bool_expr(e)
344-
} else {
345-
Expression::Other
346-
}
347-
} else {
348-
Expression::Other
349-
}
350-
},
351-
_ => Expression::Other,
331+
fn fetch_bool_block(expr: &Expr<'_>) -> Option<Expression> {
332+
match peel_blocks_with_stmt(expr).kind {
333+
ExprKind::Ret(Some(ret)) => Some(Expression::RetBool(fetch_bool_expr(ret)?)),
334+
_ => Some(Expression::Bool(fetch_bool_expr(expr)?)),
352335
}
353336
}
354337

355-
fn fetch_bool_expr(expr: &Expr<'_>) -> Expression {
356-
match expr.kind {
357-
ExprKind::Block(block, _) => fetch_bool_block(block),
358-
ExprKind::Lit(ref lit_ptr) => {
359-
if let LitKind::Bool(value) = lit_ptr.node {
360-
Expression::Bool(value)
361-
} else {
362-
Expression::Other
363-
}
364-
},
365-
ExprKind::Ret(Some(expr)) => match fetch_bool_expr(expr) {
366-
Expression::Bool(value) => Expression::RetBool(value),
367-
_ => Expression::Other,
368-
},
369-
_ => Expression::Other,
338+
fn fetch_bool_expr(expr: &Expr<'_>) -> Option<bool> {
339+
if let ExprKind::Lit(ref lit_ptr) = peel_blocks(expr).kind {
340+
if let LitKind::Bool(value) = lit_ptr.node {
341+
return Some(value);
342+
}
370343
}
344+
None
371345
}

0 commit comments

Comments
 (0)