1 //! Checks for needless boolean results of if-else expressions
3 //! This lint is **warn** by default
7 use syntax::ast::LitKind;
8 use syntax::codemap::Spanned;
9 use utils::{span_lint, span_lint_and_then, snippet};
10 use utils::sugg::Sugg;
12 /// **What it does:** Checks for expressions of the form `if c { true } else { false }`
13 /// (or vice versa) and suggest using the condition directly.
15 /// **Why is this bad?** Redundant code.
17 /// **Known problems:** Maybe false positives: Sometimes, the two branches are
18 /// painstakingly documented (which we of course do not detect), so they *may*
19 /// have some value. Even then, the documentation can be rewritten to match the
24 /// if x { false } else { true }
29 "if-statements with plain booleans in the then- and else-clause, e.g. \
30 `if p { true } else { false }`"
33 /// **What it does:** Checks for expressions of the form `x == true` (or vice
34 /// versa) and suggest using the variable directly.
36 /// **Why is this bad?** Unnecessary code.
38 /// **Known problems:** None.
42 /// if x == true { } // could be `if x { }`
47 "comparing a variable to a boolean, e.g. `if x == true`"
51 pub struct NeedlessBool;
53 impl LintPass for NeedlessBool {
54 fn get_lints(&self) -> LintArray {
55 lint_array!(NEEDLESS_BOOL)
59 impl LateLintPass for NeedlessBool {
60 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
61 use self::Expression::*;
62 if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node {
63 let reduce = |ret, not| {
64 let snip = Sugg::hir(cx, pred, "<predicate>");
72 format!("return {}", snip)
77 span_lint_and_then(cx,
80 "this if-then-else expression returns a bool literal",
82 db.span_suggestion(e.span, "you can reduce it to", hint);
85 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
86 (RetBool(true), RetBool(true)) |
87 (Bool(true), Bool(true)) => {
91 "this if-then-else expression will always return true");
93 (RetBool(false), RetBool(false)) |
94 (Bool(false), Bool(false)) => {
98 "this if-then-else expression will always return false");
100 (RetBool(true), RetBool(false)) => reduce(true, false),
101 (Bool(true), Bool(false)) => reduce(false, false),
102 (RetBool(false), RetBool(true)) => reduce(true, true),
103 (Bool(false), Bool(true)) => reduce(false, true),
110 #[derive(Copy,Clone)]
111 pub struct BoolComparison;
113 impl LintPass for BoolComparison {
114 fn get_lints(&self) -> LintArray {
115 lint_array!(BOOL_COMPARISON)
119 impl LateLintPass for BoolComparison {
120 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
121 use self::Expression::*;
122 if let ExprBinary(Spanned { node: BiEq, .. }, ref left_side, ref right_side) = e.node {
123 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
124 (Bool(true), Other) => {
125 let hint = snippet(cx, right_side.span, "..").into_owned();
126 span_lint_and_then(cx,
129 "equality checks against true are unnecessary",
131 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
134 (Other, Bool(true)) => {
135 let hint = snippet(cx, left_side.span, "..").into_owned();
136 span_lint_and_then(cx,
139 "equality checks against true are unnecessary",
141 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
144 (Bool(false), Other) => {
145 let hint = Sugg::hir(cx, right_side, "..");
146 span_lint_and_then(cx,
149 "equality checks against false can be replaced by a negation",
151 db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
154 (Other, Bool(false)) => {
155 let hint = Sugg::hir(cx, left_side, "..");
156 span_lint_and_then(cx,
159 "equality checks against false can be replaced by a negation",
161 db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
176 fn fetch_bool_block(block: &Block) -> Expression {
177 match (&*block.stmts, block.expr.as_ref()) {
178 (&[], Some(e)) => fetch_bool_expr(&**e),
179 (&[ref e], None) => {
180 if let StmtSemi(ref e, _) = e.node {
181 if let ExprRet(_) = e.node {
182 fetch_bool_expr(&**e)
190 _ => Expression::Other,
194 fn fetch_bool_expr(expr: &Expr) -> Expression {
196 ExprBlock(ref block) => fetch_bool_block(block),
197 ExprLit(ref lit_ptr) => {
198 if let LitKind::Bool(value) = lit_ptr.node {
199 Expression::Bool(value)
204 ExprRet(Some(ref expr)) => {
205 match fetch_bool_expr(expr) {
206 Expression::Bool(value) => Expression::RetBool(value),
207 _ => Expression::Other,
210 _ => Expression::Other,