1 //! Checks for needless boolean results of if-else expressions
3 //! This lint is **warn** by default
6 use rustc::{declare_lint, lint_array};
8 use syntax::ast::LitKind;
9 use syntax::source_map::Spanned;
10 use crate::utils::{snippet, span_lint, span_lint_and_sugg};
11 use crate::utils::sugg::Sugg;
13 /// **What it does:** Checks for expressions of the form `if c { true } else {
15 /// (or vice versa) and suggest using the condition directly.
17 /// **Why is this bad?** Redundant code.
19 /// **Known problems:** Maybe false positives: Sometimes, the two branches are
20 /// painstakingly documented (which we of course do not detect), so they *may*
21 /// have some value. Even then, the documentation can be rewritten to match the
26 /// if x { false } else { true }
28 declare_clippy_lint! {
31 "if-statements with plain booleans in the then- and else-clause, e.g. \
32 `if p { true } else { false }`"
35 /// **What it does:** Checks for expressions of the form `x == true` (or vice
36 /// versa) and suggest using the variable directly.
38 /// **Why is this bad?** Unnecessary code.
40 /// **Known problems:** None.
44 /// if x == true { } // could be `if x { }`
46 declare_clippy_lint! {
49 "comparing a variable to a boolean, e.g. `if x == true`"
52 #[derive(Copy, Clone)]
53 pub struct NeedlessBool;
55 impl LintPass for NeedlessBool {
56 fn get_lints(&self) -> LintArray {
57 lint_array!(NEEDLESS_BOOL)
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
62 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
63 use self::Expression::*;
64 if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
65 let reduce = |ret, not| {
66 let snip = Sugg::hir(cx, pred, "<predicate>");
67 let snip = if not { !snip } else { snip };
70 format!("return {}", snip)
79 "this if-then-else expression returns a bool literal",
80 "you can reduce it to",
84 if let ExprKind::Block(ref then_block, _) = then_block.node {
85 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
86 (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
91 "this if-then-else expression will always return true",
94 (RetBool(false), RetBool(false)) | (Bool(false), Bool(false)) => {
99 "this if-then-else expression will always return false",
102 (RetBool(true), RetBool(false)) => reduce(true, false),
103 (Bool(true), Bool(false)) => reduce(false, false),
104 (RetBool(false), RetBool(true)) => reduce(true, true),
105 (Bool(false), Bool(true)) => reduce(false, true),
109 panic!("IfExpr 'then' node is not an ExprKind::Block");
115 #[derive(Copy, Clone)]
116 pub struct BoolComparison;
118 impl LintPass for BoolComparison {
119 fn get_lints(&self) -> LintArray {
120 lint_array!(BOOL_COMPARISON)
124 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
125 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
126 use self::Expression::*;
127 if let ExprKind::Binary(Spanned { node: BinOpKind::Eq, .. }, ref left_side, ref right_side) = e.node {
128 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
129 (Bool(true), Other) => {
130 let hint = snippet(cx, right_side.span, "..").into_owned();
135 "equality checks against true are unnecessary",
136 "try simplifying it as shown",
140 (Other, Bool(true)) => {
141 let hint = snippet(cx, left_side.span, "..").into_owned();
146 "equality checks against true are unnecessary",
147 "try simplifying it as shown",
151 (Bool(false), Other) => {
152 let hint = Sugg::hir(cx, right_side, "..");
157 "equality checks against false can be replaced by a negation",
158 "try simplifying it as shown",
162 (Other, Bool(false)) => {
163 let hint = Sugg::hir(cx, left_side, "..");
168 "equality checks against false can be replaced by a negation",
169 "try simplifying it as shown",
185 fn fetch_bool_block(block: &Block) -> Expression {
186 match (&*block.stmts, block.expr.as_ref()) {
187 (&[], Some(e)) => fetch_bool_expr(&**e),
188 (&[ref e], None) => if let StmtKind::Semi(ref e, _) = e.node {
189 if let ExprKind::Ret(_) = e.node {
190 fetch_bool_expr(&**e)
197 _ => Expression::Other,
201 fn fetch_bool_expr(expr: &Expr) -> Expression {
203 ExprKind::Block(ref block, _) => fetch_bool_block(block),
204 ExprKind::Lit(ref lit_ptr) => if let LitKind::Bool(value) = lit_ptr.node {
205 Expression::Bool(value)
209 ExprKind::Ret(Some(ref expr)) => match fetch_bool_expr(expr) {
210 Expression::Bool(value) => Expression::RetBool(value),
211 _ => Expression::Other,
213 _ => Expression::Other,