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, snippet_opt};
11 /// **What it does:** This lint checks for expressions of the form `if c { true } else { false }` (or vice versa) and suggest using the condition directly.
13 /// **Why is this bad?** Redundant code.
15 /// **Known problems:** Maybe false positives: Sometimes, the two branches are painstakingly documented (which we of course do not detect), so they *may* have some value. Even then, the documentation can be rewritten to match the shorter code.
17 /// **Example:** `if x { false } else { true }`
21 "if-statements with plain booleans in the then- and else-clause, e.g. \
22 `if p { true } else { false }`"
25 /// **What it does:** This lint checks for expressions of the form `x == true` (or vice versa) and suggest using the variable directly.
27 /// **Why is this bad?** Unnecessary code.
29 /// **Known problems:** None.
31 /// **Example:** `if x == true { }` could be `if x { }`
35 "comparing a variable to a boolean, e.g. \
40 pub struct NeedlessBool;
42 impl LintPass for NeedlessBool {
43 fn get_lints(&self) -> LintArray {
44 lint_array!(NEEDLESS_BOOL)
48 impl LateLintPass for NeedlessBool {
49 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
50 use self::Expression::*;
51 if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node {
52 let reduce = |hint: &str, not| {
53 let hint = match snippet_opt(cx, pred.span) {
54 Some(pred_snip) => format!("`{}{}`", not, pred_snip),
57 span_lint_and_then(cx,
60 "this if-then-else expression returns a bool literal",
62 db.span_suggestion(e.span, "you can reduce it to", hint);
65 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
66 (RetBool(true), RetBool(true)) |
67 (Bool(true), Bool(true)) => {
71 "this if-then-else expression will always return true");
73 (RetBool(false), RetBool(false)) |
74 (Bool(false), Bool(false)) => {
78 "this if-then-else expression will always return false");
80 (RetBool(true), RetBool(false)) => reduce("its predicate", "return "),
81 (Bool(true), Bool(false)) => reduce("its predicate", ""),
82 (RetBool(false), RetBool(true)) => reduce("`!` and its predicate", "return !"),
83 (Bool(false), Bool(true)) => reduce("`!` and its predicate", "!"),
91 pub struct BoolComparison;
93 impl LintPass for BoolComparison {
94 fn get_lints(&self) -> LintArray {
95 lint_array!(BOOL_COMPARISON)
99 impl LateLintPass for BoolComparison {
100 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
101 use self::Expression::*;
102 if let ExprBinary(Spanned { node: BiEq, .. }, ref left_side, ref right_side) = e.node {
103 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
104 (Bool(true), Other) => {
105 let hint = snippet(cx, right_side.span, "..").into_owned();
106 span_lint_and_then(cx,
109 "equality checks against true are unnecessary",
111 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
114 (Other, Bool(true)) => {
115 let hint = snippet(cx, left_side.span, "..").into_owned();
116 span_lint_and_then(cx,
119 "equality checks against true are unnecessary",
121 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
124 (Bool(false), Other) => {
125 let hint = format!("!{}", snippet(cx, right_side.span, ".."));
126 span_lint_and_then(cx,
129 "equality checks against false can be replaced by a negation",
131 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
134 (Other, Bool(false)) => {
135 let hint = format!("!{}", snippet(cx, left_side.span, ".."));
136 span_lint_and_then(cx,
139 "equality checks against false can be replaced by a negation",
141 db.span_suggestion(e.span, "try simplifying it as shown:", hint);
156 fn fetch_bool_block(block: &Block) -> Expression {
157 match (&*block.stmts, block.expr.as_ref()) {
158 (&[], Some(e)) => fetch_bool_expr(&**e),
159 (&[ref e], None) => {
160 if let StmtSemi(ref e, _) = e.node {
161 if let ExprRet(_) = e.node {
162 fetch_bool_expr(&**e)
170 _ => Expression::Other,
174 fn fetch_bool_expr(expr: &Expr) -> Expression {
176 ExprBlock(ref block) => fetch_bool_block(block),
177 ExprLit(ref lit_ptr) => {
178 if let LitKind::Bool(value) = lit_ptr.node {
179 Expression::Bool(value)
184 ExprRet(Some(ref expr)) => {
185 match fetch_bool_expr(expr) {
186 Expression::Bool(value) => Expression::RetBool(value),
187 _ => Expression::Other,
190 _ => Expression::Other,