1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! Checks for needless boolean results of if-else expressions
13 //! This lint is **warn** by default
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use crate::rustc::hir::*;
18 use crate::syntax::ast::LitKind;
19 use crate::syntax::source_map::Spanned;
20 use crate::utils::{in_macro, snippet, span_lint, span_lint_and_sugg};
21 use crate::utils::sugg::Sugg;
23 /// **What it does:** Checks for expressions of the form `if c { true } else {
25 /// (or vice versa) and suggest using the condition directly.
27 /// **Why is this bad?** Redundant code.
29 /// **Known problems:** Maybe false positives: Sometimes, the two branches are
30 /// painstakingly documented (which we of course do not detect), so they *may*
31 /// have some value. Even then, the documentation can be rewritten to match the
36 /// if x { false } else { true }
38 declare_clippy_lint! {
41 "if-statements with plain booleans in the then- and else-clause, e.g. \
42 `if p { true } else { false }`"
45 /// **What it does:** Checks for expressions of the form `x == true` (or vice
46 /// versa) and suggest using the variable directly.
48 /// **Why is this bad?** Unnecessary code.
50 /// **Known problems:** None.
54 /// if x == true { } // could be `if x { }`
56 declare_clippy_lint! {
59 "comparing a variable to a boolean, e.g. `if x == true`"
62 #[derive(Copy, Clone)]
63 pub struct NeedlessBool;
65 impl LintPass for NeedlessBool {
66 fn get_lints(&self) -> LintArray {
67 lint_array!(NEEDLESS_BOOL)
71 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
72 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
73 use self::Expression::*;
74 if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
75 let reduce = |ret, not| {
76 let snip = Sugg::hir(cx, pred, "<predicate>");
77 let snip = if not { !snip } else { snip };
80 format!("return {}", snip)
89 "this if-then-else expression returns a bool literal",
90 "you can reduce it to",
94 if let ExprKind::Block(ref then_block, _) = then_block.node {
95 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
96 (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
101 "this if-then-else expression will always return true",
104 (RetBool(false), RetBool(false)) | (Bool(false), Bool(false)) => {
109 "this if-then-else expression will always return false",
112 (RetBool(true), RetBool(false)) => reduce(true, false),
113 (Bool(true), Bool(false)) => reduce(false, false),
114 (RetBool(false), RetBool(true)) => reduce(true, true),
115 (Bool(false), Bool(true)) => reduce(false, true),
119 panic!("IfExpr 'then' node is not an ExprKind::Block");
125 #[derive(Copy, Clone)]
126 pub struct BoolComparison;
128 impl LintPass for BoolComparison {
129 fn get_lints(&self) -> LintArray {
130 lint_array!(BOOL_COMPARISON)
134 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
135 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
136 if in_macro(e.span) {
139 use self::Expression::*;
140 if let ExprKind::Binary(Spanned { node: BinOpKind::Eq, .. }, ref left_side, ref right_side) = e.node {
141 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
142 (Bool(true), Other) => {
143 let hint = snippet(cx, right_side.span, "..").into_owned();
148 "equality checks against true are unnecessary",
149 "try simplifying it as shown",
153 (Other, Bool(true)) => {
154 let hint = snippet(cx, left_side.span, "..").into_owned();
159 "equality checks against true are unnecessary",
160 "try simplifying it as shown",
164 (Bool(false), Other) => {
165 let hint = Sugg::hir(cx, right_side, "..");
170 "equality checks against false can be replaced by a negation",
171 "try simplifying it as shown",
175 (Other, Bool(false)) => {
176 let hint = Sugg::hir(cx, left_side, "..");
181 "equality checks against false can be replaced by a negation",
182 "try simplifying it as shown",
198 fn fetch_bool_block(block: &Block) -> Expression {
199 match (&*block.stmts, block.expr.as_ref()) {
200 (&[], Some(e)) => fetch_bool_expr(&**e),
201 (&[ref e], None) => if let StmtKind::Semi(ref e, _) = e.node {
202 if let ExprKind::Ret(_) = e.node {
203 fetch_bool_expr(&**e)
210 _ => Expression::Other,
214 fn fetch_bool_expr(expr: &Expr) -> Expression {
216 ExprKind::Block(ref block, _) => fetch_bool_block(block),
217 ExprKind::Lit(ref lit_ptr) => if let LitKind::Bool(value) = lit_ptr.node {
218 Expression::Bool(value)
222 ExprKind::Ret(Some(ref expr)) => match fetch_bool_expr(expr) {
223 Expression::Bool(value) => Expression::RetBool(value),
224 _ => Expression::Other,
226 _ => Expression::Other,