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::{snippet, span_lint, span_lint_and_sugg};
10 use utils::sugg::Sugg;
12 /// **What it does:** Checks for expressions of the form `if c { true } else {
14 /// (or vice versa) and suggest using the condition directly.
16 /// **Why is this bad?** Redundant code.
18 /// **Known problems:** Maybe false positives: Sometimes, the two branches are
19 /// painstakingly documented (which we of course do not detect), so they *may*
20 /// have some value. Even then, the documentation can be rewritten to match the
25 /// if x { false } else { true }
30 "if-statements with plain booleans in the then- and else-clause, e.g. \
31 `if p { true } else { false }`"
34 /// **What it does:** Checks for expressions of the form `x == true` (or vice
35 /// versa) and suggest using the variable directly.
37 /// **Why is this bad?** Unnecessary code.
39 /// **Known problems:** None.
43 /// if x == true { } // could be `if x { }`
48 "comparing a variable to a boolean, e.g. `if x == true`"
51 #[derive(Copy, Clone)]
52 pub struct NeedlessBool;
54 impl LintPass for NeedlessBool {
55 fn get_lints(&self) -> LintArray {
56 lint_array!(NEEDLESS_BOOL)
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
61 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
62 use self::Expression::*;
63 if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node {
64 let reduce = |ret, not| {
65 let snip = Sugg::hir(cx, pred, "<predicate>");
66 let snip = if not { !snip } else { snip };
69 format!("return {}", snip)
78 "this if-then-else expression returns a bool literal",
79 "you can reduce it to",
83 if let ExprBlock(ref then_block) = then_block.node {
84 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
85 (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
90 "this if-then-else expression will always return true",
93 (RetBool(false), RetBool(false)) | (Bool(false), Bool(false)) => {
98 "this if-then-else expression will always return false",
101 (RetBool(true), RetBool(false)) => reduce(true, false),
102 (Bool(true), Bool(false)) => reduce(false, false),
103 (RetBool(false), RetBool(true)) => reduce(true, true),
104 (Bool(false), Bool(true)) => reduce(false, true),
108 panic!("IfExpr 'then' node is not an ExprBlock");
114 #[derive(Copy, Clone)]
115 pub struct BoolComparison;
117 impl LintPass for BoolComparison {
118 fn get_lints(&self) -> LintArray {
119 lint_array!(BOOL_COMPARISON)
123 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
124 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
125 use self::Expression::*;
126 if let ExprBinary(Spanned { node: BiEq, .. }, ref left_side, ref right_side) = e.node {
127 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
128 (Bool(true), Other) => {
129 let hint = snippet(cx, right_side.span, "..").into_owned();
134 "equality checks against true are unnecessary",
135 "try simplifying it as shown",
139 (Other, Bool(true)) => {
140 let hint = snippet(cx, left_side.span, "..").into_owned();
145 "equality checks against true are unnecessary",
146 "try simplifying it as shown",
150 (Bool(false), Other) => {
151 let hint = Sugg::hir(cx, right_side, "..");
156 "equality checks against false can be replaced by a negation",
157 "try simplifying it as shown",
161 (Other, Bool(false)) => {
162 let hint = Sugg::hir(cx, left_side, "..");
167 "equality checks against false can be replaced by a negation",
168 "try simplifying it as shown",
184 fn fetch_bool_block(block: &Block) -> Expression {
185 match (&*block.stmts, block.expr.as_ref()) {
186 (&[], Some(e)) => fetch_bool_expr(&**e),
187 (&[ref e], None) => if let StmtSemi(ref e, _) = e.node {
188 if let ExprRet(_) = e.node {
189 fetch_bool_expr(&**e)
196 _ => Expression::Other,
200 fn fetch_bool_expr(expr: &Expr) -> Expression {
202 ExprBlock(ref block) => fetch_bool_block(block),
203 ExprLit(ref lit_ptr) => if let LitKind::Bool(value) = lit_ptr.node {
204 Expression::Bool(value)
208 ExprRet(Some(ref expr)) => match fetch_bool_expr(expr) {
209 Expression::Bool(value) => Expression::RetBool(value),
210 _ => Expression::Other,
212 _ => Expression::Other,