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.
10 //! Checks for needless boolean results of if-else expressions
12 //! This lint is **warn** by default
14 use crate::rustc::hir::*;
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use crate::rustc_errors::Applicability;
18 use crate::syntax::ast::LitKind;
19 use crate::syntax::source_map::Spanned;
20 use crate::utils::sugg::Sugg;
21 use crate::utils::{in_macro, snippet_with_applicability, span_lint, span_lint_and_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
42 declare_clippy_lint! {
45 "if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`"
48 /// **What it does:** Checks for expressions of the form `x == true` (or vice
49 /// versa) and suggest using the variable directly.
51 /// **Why is this bad?** Unnecessary code.
53 /// **Known problems:** None.
57 /// if x == true {} // could be `if x { }`
59 declare_clippy_lint! {
62 "comparing a variable to a boolean, e.g. `if x == true`"
65 #[derive(Copy, Clone)]
66 pub struct NeedlessBool;
68 impl LintPass for NeedlessBool {
69 fn get_lints(&self) -> LintArray {
70 lint_array!(NEEDLESS_BOOL)
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
75 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
76 use self::Expression::*;
77 if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
78 let reduce = |ret, not| {
79 let mut applicability = Applicability::MachineApplicable;
80 let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
81 let snip = if not { !snip } else { snip };
84 format!("return {}", snip)
93 "this if-then-else expression returns a bool literal",
94 "you can reduce it to",
99 if let ExprKind::Block(ref then_block, _) = then_block.node {
100 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
101 (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
106 "this if-then-else expression will always return true",
109 (RetBool(false), RetBool(false)) | (Bool(false), Bool(false)) => {
114 "this if-then-else expression will always return false",
117 (RetBool(true), RetBool(false)) => reduce(true, false),
118 (Bool(true), Bool(false)) => reduce(false, false),
119 (RetBool(false), RetBool(true)) => reduce(true, true),
120 (Bool(false), Bool(true)) => reduce(false, true),
124 panic!("IfExpr 'then' node is not an ExprKind::Block");
130 #[derive(Copy, Clone)]
131 pub struct BoolComparison;
133 impl LintPass for BoolComparison {
134 fn get_lints(&self) -> LintArray {
135 lint_array!(BOOL_COMPARISON)
139 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
140 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
141 use self::Expression::*;
143 if in_macro(e.span) {
147 if let ExprKind::Binary(
149 node: BinOpKind::Eq, ..
155 let mut applicability = Applicability::MachineApplicable;
156 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
157 (Bool(true), Other) => {
158 let hint = snippet_with_applicability(cx, right_side.span, "..", &mut applicability);
163 "equality checks against true are unnecessary",
164 "try simplifying it as shown",
169 (Other, Bool(true)) => {
170 let hint = snippet_with_applicability(cx, left_side.span, "..", &mut applicability);
175 "equality checks against true are unnecessary",
176 "try simplifying it as shown",
181 (Bool(false), Other) => {
182 let hint = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
187 "equality checks against false can be replaced by a negation",
188 "try simplifying it as shown",
193 (Other, Bool(false)) => {
194 let hint = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
199 "equality checks against false can be replaced by a negation",
200 "try simplifying it as shown",
217 fn fetch_bool_block(block: &Block) -> Expression {
218 match (&*block.stmts, block.expr.as_ref()) {
219 (&[], Some(e)) => fetch_bool_expr(&**e),
220 (&[ref e], None) => {
221 if let StmtKind::Semi(ref e, _) = e.node {
222 if let ExprKind::Ret(_) = e.node {
223 fetch_bool_expr(&**e)
231 _ => Expression::Other,
235 fn fetch_bool_expr(expr: &Expr) -> Expression {
237 ExprKind::Block(ref block, _) => fetch_bool_block(block),
238 ExprKind::Lit(ref lit_ptr) => {
239 if let LitKind::Bool(value) = lit_ptr.node {
240 Expression::Bool(value)
245 ExprKind::Ret(Some(ref expr)) => match fetch_bool_expr(expr) {
246 Expression::Bool(value) => Expression::RetBool(value),
247 _ => Expression::Other,
249 _ => Expression::Other,