1 //! Checks for needless boolean results of if-else expressions
3 //! This lint is **warn** by default
5 use crate::utils::sugg::Sugg;
6 use crate::utils::{higher, parent_node_is_if_expr, span_lint, span_lint_and_sugg};
7 use rustc::declare_lint_pass;
9 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
10 use rustc_errors::Applicability;
11 use rustc_session::declare_tool_lint;
12 use syntax::ast::LitKind;
13 use syntax::source_map::Spanned;
15 declare_clippy_lint! {
16 /// **What it does:** Checks for expressions of the form `if c { true } else {
18 /// (or vice versa) and suggest using the condition directly.
20 /// **Why is this bad?** Redundant code.
22 /// **Known problems:** Maybe false positives: Sometimes, the two branches are
23 /// painstakingly documented (which we, of course, do not detect), so they *may*
24 /// have some value. Even then, the documentation can be rewritten to match the
35 /// Could be written as
41 "if-statements with plain booleans in the then- and else-clause, e.g., `if p { true } else { false }`"
44 declare_clippy_lint! {
45 /// **What it does:** Checks for expressions of the form `x == true`,
46 /// `x != true` and order comparisons such as `x < true` (or vice versa) and
47 /// suggest using the variable directly.
49 /// **Why is this bad?** Unnecessary code.
51 /// **Known problems:** None.
55 /// if x == true {} // could be `if x { }`
59 "comparing a variable to a boolean, e.g., `if x == true` or `if x != true`"
62 declare_lint_pass!(NeedlessBool => [NEEDLESS_BOOL]);
64 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
65 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
66 use self::Expression::*;
67 if let Some((ref pred, ref then_block, Some(ref else_expr))) = higher::if_block(&e) {
68 let reduce = |ret, not| {
69 let mut applicability = Applicability::MachineApplicable;
70 let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
71 let mut snip = if not { !snip } else { snip };
74 snip = snip.make_return();
77 if parent_node_is_if_expr(&e, &cx) {
78 snip = snip.blockify()
85 "this if-then-else expression returns a bool literal",
86 "you can reduce it to",
91 if let ExprKind::Block(ref then_block, _) = then_block.kind {
92 match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) {
93 (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
98 "this if-then-else expression will always return true",
101 (RetBool(false), RetBool(false)) | (Bool(false), Bool(false)) => {
106 "this if-then-else expression will always return false",
109 (RetBool(true), RetBool(false)) => reduce(true, false),
110 (Bool(true), Bool(false)) => reduce(false, false),
111 (RetBool(false), RetBool(true)) => reduce(true, true),
112 (Bool(false), Bool(true)) => reduce(false, true),
116 panic!("IfExpr 'then' node is not an ExprKind::Block");
122 declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);
124 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
125 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
126 if e.span.from_expansion() {
130 if let ExprKind::Binary(Spanned { node, .. }, ..) = e.kind {
131 let ignore_case = None::<(fn(_) -> _, &str)>;
132 let ignore_no_literal = None::<(fn(_, _) -> _, &str)>;
135 let true_case = Some((|h| h, "equality checks against true are unnecessary"));
136 let false_case = Some((
138 "equality checks against false can be replaced by a negation",
140 check_comparison(cx, e, true_case, false_case, true_case, false_case, ignore_no_literal)
143 let true_case = Some((
145 "inequality checks against true can be replaced by a negation",
147 let false_case = Some((|h| h, "inequality checks against false are unnecessary"));
148 check_comparison(cx, e, true_case, false_case, true_case, false_case, ignore_no_literal)
150 BinOpKind::Lt => check_comparison(
154 Some((|h| h, "greater than checks against false are unnecessary")),
157 "less than comparison against true can be replaced by a negation",
161 |l: Sugg<'_>, r: Sugg<'_>| (!l).bit_and(&r),
162 "order comparisons between booleans can be simplified",
165 BinOpKind::Gt => check_comparison(
170 "less than comparison against true can be replaced by a negation",
174 Some((|h| h, "greater than checks against false are unnecessary")),
176 |l: Sugg<'_>, r: Sugg<'_>| l.bit_and(&(!r)),
177 "order comparisons between booleans can be simplified",
186 fn check_comparison<'a, 'tcx>(
187 cx: &LateContext<'a, 'tcx>,
189 left_true: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
190 left_false: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
191 right_true: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
192 right_false: Option<(impl FnOnce(Sugg<'a>) -> Sugg<'a>, &str)>,
193 no_literal: Option<(impl FnOnce(Sugg<'a>, Sugg<'a>) -> Sugg<'a>, &str)>,
195 use self::Expression::*;
197 if let ExprKind::Binary(_, ref left_side, ref right_side) = e.kind {
198 let (l_ty, r_ty) = (cx.tables.expr_ty(left_side), cx.tables.expr_ty(right_side));
199 if l_ty.is_bool() && r_ty.is_bool() {
200 let mut applicability = Applicability::MachineApplicable;
201 match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
202 (Bool(true), Other) => left_true.map_or((), |(h, m)| {
203 suggest_bool_comparison(cx, e, right_side, applicability, m, h)
205 (Other, Bool(true)) => right_true.map_or((), |(h, m)| {
206 suggest_bool_comparison(cx, e, left_side, applicability, m, h)
208 (Bool(false), Other) => left_false.map_or((), |(h, m)| {
209 suggest_bool_comparison(cx, e, right_side, applicability, m, h)
211 (Other, Bool(false)) => right_false.map_or((), |(h, m)| {
212 suggest_bool_comparison(cx, e, left_side, applicability, m, h)
214 (Other, Other) => no_literal.map_or((), |(h, m)| {
215 let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
216 let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
222 "try simplifying it as shown",
223 h(left_side, right_side).to_string(),
233 fn suggest_bool_comparison<'a, 'tcx>(
234 cx: &LateContext<'a, 'tcx>,
237 mut applicability: Applicability,
239 conv_hint: impl FnOnce(Sugg<'a>) -> Sugg<'a>,
241 let hint = Sugg::hir_with_applicability(cx, expr, "..", &mut applicability);
247 "try simplifying it as shown",
248 conv_hint(hint).to_string(),
259 fn fetch_bool_block(block: &Block) -> Expression {
260 match (&*block.stmts, block.expr.as_ref()) {
261 (&[], Some(e)) => fetch_bool_expr(&**e),
262 (&[ref e], None) => {
263 if let StmtKind::Semi(ref e) = e.kind {
264 if let ExprKind::Ret(_) = e.kind {
265 fetch_bool_expr(&**e)
273 _ => Expression::Other,
277 fn fetch_bool_expr(expr: &Expr) -> Expression {
279 ExprKind::Block(ref block, _) => fetch_bool_block(block),
280 ExprKind::Lit(ref lit_ptr) => {
281 if let LitKind::Bool(value) = lit_ptr.node {
282 Expression::Bool(value)
287 ExprKind::Ret(Some(ref expr)) => match fetch_bool_expr(expr) {
288 Expression::Bool(value) => Expression::RetBool(value),
289 _ => Expression::Other,
291 _ => Expression::Other,