Skip to content

Commit 16bbd24

Browse files
committed
Simplify needless_bool
1 parent f690ef6 commit 16bbd24

File tree

1 file changed

+19
-44
lines changed

1 file changed

+19
-44
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
66
use clippy_utils::higher;
77
use clippy_utils::source::snippet_with_applicability;
88
use clippy_utils::sugg::Sugg;
9-
use clippy_utils::{get_parent_node, is_else_clause, is_expn_of};
9+
use clippy_utils::{get_parent_node, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt};
1010
use rustc_ast::ast::LitKind;
1111
use rustc_errors::Applicability;
12-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, StmtKind, UnOp};
12+
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, UnOp};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_session::{declare_lint_pass, declare_tool_lint};
1515
use rustc_span::source_map::Spanned;
@@ -143,8 +143,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
143143
applicability,
144144
);
145145
};
146-
if let ExprKind::Block(then, _) = then.kind {
147-
match (fetch_bool_block(then), fetch_bool_expr(r#else)) {
146+
if let Some((a, b)) = fetch_bool_block(then).and_then(|a| Some((a, fetch_bool_block(r#else)?))) {
147+
match (a, b) {
148148
(RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
149149
span_lint(
150150
cx,
@@ -167,8 +167,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
167167
(Bool(false), Bool(true)) => reduce(false, true),
168168
_ => (),
169169
}
170-
} else {
171-
panic!("IfExpr `then` node is not an `ExprKind::Block`");
172170
}
173171
}
174172
}
@@ -271,8 +269,6 @@ fn check_comparison<'a, 'tcx>(
271269
right_false: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
272270
no_literal: Option<(impl FnOnce(Sugg<'a>, Sugg<'a>) -> Sugg<'a>, &str)>,
273271
) {
274-
use self::Expression::{Bool, Other};
275-
276272
if let ExprKind::Binary(op, left_side, right_side) = e.kind {
277273
let (l_ty, r_ty) = (
278274
cx.typeck_results().expr_ty(left_side),
@@ -304,19 +300,19 @@ fn check_comparison<'a, 'tcx>(
304300
}
305301

306302
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
307-
(Bool(true), Other) => left_true.map_or((), |(h, m)| {
303+
(Some(true), None) => left_true.map_or((), |(h, m)| {
308304
suggest_bool_comparison(cx, e, right_side, applicability, m, h);
309305
}),
310-
(Other, Bool(true)) => right_true.map_or((), |(h, m)| {
306+
(None, Some(true)) => right_true.map_or((), |(h, m)| {
311307
suggest_bool_comparison(cx, e, left_side, applicability, m, h);
312308
}),
313-
(Bool(false), Other) => left_false.map_or((), |(h, m)| {
309+
(Some(false), None) => left_false.map_or((), |(h, m)| {
314310
suggest_bool_comparison(cx, e, right_side, applicability, m, h);
315311
}),
316-
(Other, Bool(false)) => right_false.map_or((), |(h, m)| {
312+
(None, Some(false)) => right_false.map_or((), |(h, m)| {
317313
suggest_bool_comparison(cx, e, left_side, applicability, m, h);
318314
}),
319-
(Other, Other) => no_literal.map_or((), |(h, m)| {
315+
(None, None) => no_literal.map_or((), |(h, m)| {
320316
let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
321317
let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
322318
span_lint_and_sugg(
@@ -365,41 +361,20 @@ fn suggest_bool_comparison<'a, 'tcx>(
365361
enum Expression {
366362
Bool(bool),
367363
RetBool(bool),
368-
Other,
369364
}
370365

371-
fn fetch_bool_block(block: &Block<'_>) -> Expression {
372-
match (&*block.stmts, block.expr.as_ref()) {
373-
(&[], Some(e)) => fetch_bool_expr(&**e),
374-
(&[ref e], None) => {
375-
if let StmtKind::Semi(e) = e.kind {
376-
if let ExprKind::Ret(_) = e.kind {
377-
fetch_bool_expr(e)
378-
} else {
379-
Expression::Other
380-
}
381-
} else {
382-
Expression::Other
383-
}
384-
},
385-
_ => Expression::Other,
366+
fn fetch_bool_block(expr: &Expr<'_>) -> Option<Expression> {
367+
match peel_blocks_with_stmt(expr).kind {
368+
ExprKind::Ret(Some(ret)) => Some(Expression::RetBool(fetch_bool_expr(ret)?)),
369+
_ => Some(Expression::Bool(fetch_bool_expr(expr)?)),
386370
}
387371
}
388372

389-
fn fetch_bool_expr(expr: &Expr<'_>) -> Expression {
390-
match expr.kind {
391-
ExprKind::Block(block, _) => fetch_bool_block(block),
392-
ExprKind::Lit(ref lit_ptr) => {
393-
if let LitKind::Bool(value) = lit_ptr.node {
394-
Expression::Bool(value)
395-
} else {
396-
Expression::Other
397-
}
398-
},
399-
ExprKind::Ret(Some(expr)) => match fetch_bool_expr(expr) {
400-
Expression::Bool(value) => Expression::RetBool(value),
401-
_ => Expression::Other,
402-
},
403-
_ => Expression::Other,
373+
fn fetch_bool_expr(expr: &Expr<'_>) -> Option<bool> {
374+
if let ExprKind::Lit(ref lit_ptr) = peel_blocks(expr).kind {
375+
if let LitKind::Bool(value) = lit_ptr.node {
376+
return Some(value);
377+
}
404378
}
379+
None
405380
}

0 commit comments

Comments
 (0)