From 16bbd243f9e5c71ff0481c7168a32f4f442c582f Mon Sep 17 00:00:00 2001 From: Cameron Steffen Date: Wed, 24 Mar 2021 19:17:25 -0500 Subject: [PATCH] Simplify needless_bool --- clippy_lints/src/needless_bool.rs | 63 ++++++++++--------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index e780943625272..d391fbecf82e1 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -6,10 +6,10 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; use clippy_utils::higher; use clippy_utils::source::snippet_with_applicability; use clippy_utils::sugg::Sugg; -use clippy_utils::{get_parent_node, is_else_clause, is_expn_of}; +use clippy_utils::{get_parent_node, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt}; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; -use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, StmtKind, UnOp}; +use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Spanned; @@ -143,8 +143,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool { applicability, ); }; - if let ExprKind::Block(then, _) = then.kind { - match (fetch_bool_block(then), fetch_bool_expr(r#else)) { + if let Some((a, b)) = fetch_bool_block(then).and_then(|a| Some((a, fetch_bool_block(r#else)?))) { + match (a, b) { (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => { span_lint( cx, @@ -167,8 +167,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool { (Bool(false), Bool(true)) => reduce(false, true), _ => (), } - } else { - panic!("IfExpr `then` node is not an `ExprKind::Block`"); } } } @@ -271,8 +269,6 @@ fn check_comparison<'a, 'tcx>( right_false: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>, no_literal: Option<(impl FnOnce(Sugg<'a>, Sugg<'a>) -> Sugg<'a>, &str)>, ) { - use self::Expression::{Bool, Other}; - if let ExprKind::Binary(op, left_side, right_side) = e.kind { let (l_ty, r_ty) = ( cx.typeck_results().expr_ty(left_side), @@ -304,19 +300,19 @@ fn check_comparison<'a, 'tcx>( } match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) { - (Bool(true), Other) => left_true.map_or((), |(h, m)| { + (Some(true), None) => left_true.map_or((), |(h, m)| { suggest_bool_comparison(cx, e, right_side, applicability, m, h); }), - (Other, Bool(true)) => right_true.map_or((), |(h, m)| { + (None, Some(true)) => right_true.map_or((), |(h, m)| { suggest_bool_comparison(cx, e, left_side, applicability, m, h); }), - (Bool(false), Other) => left_false.map_or((), |(h, m)| { + (Some(false), None) => left_false.map_or((), |(h, m)| { suggest_bool_comparison(cx, e, right_side, applicability, m, h); }), - (Other, Bool(false)) => right_false.map_or((), |(h, m)| { + (None, Some(false)) => right_false.map_or((), |(h, m)| { suggest_bool_comparison(cx, e, left_side, applicability, m, h); }), - (Other, Other) => no_literal.map_or((), |(h, m)| { + (None, None) => no_literal.map_or((), |(h, m)| { let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability); let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability); span_lint_and_sugg( @@ -365,41 +361,20 @@ fn suggest_bool_comparison<'a, 'tcx>( enum Expression { Bool(bool), RetBool(bool), - Other, } -fn fetch_bool_block(block: &Block<'_>) -> Expression { - match (&*block.stmts, block.expr.as_ref()) { - (&[], Some(e)) => fetch_bool_expr(&**e), - (&[ref e], None) => { - if let StmtKind::Semi(e) = e.kind { - if let ExprKind::Ret(_) = e.kind { - fetch_bool_expr(e) - } else { - Expression::Other - } - } else { - Expression::Other - } - }, - _ => Expression::Other, +fn fetch_bool_block(expr: &Expr<'_>) -> Option { + match peel_blocks_with_stmt(expr).kind { + ExprKind::Ret(Some(ret)) => Some(Expression::RetBool(fetch_bool_expr(ret)?)), + _ => Some(Expression::Bool(fetch_bool_expr(expr)?)), } } -fn fetch_bool_expr(expr: &Expr<'_>) -> Expression { - match expr.kind { - ExprKind::Block(block, _) => fetch_bool_block(block), - ExprKind::Lit(ref lit_ptr) => { - if let LitKind::Bool(value) = lit_ptr.node { - Expression::Bool(value) - } else { - Expression::Other - } - }, - ExprKind::Ret(Some(expr)) => match fetch_bool_expr(expr) { - Expression::Bool(value) => Expression::RetBool(value), - _ => Expression::Other, - }, - _ => Expression::Other, +fn fetch_bool_expr(expr: &Expr<'_>) -> Option { + if let ExprKind::Lit(ref lit_ptr) = peel_blocks(expr).kind { + if let LitKind::Bool(value) = lit_ptr.node { + return Some(value); + } } + None }