1 //! lint on if branches that could be swapped so no `!` operation is necessary on the condition
7 use utils::span_help_and_lint;
9 /// **What it does:** Warns on the use of `!` or `!=` in an if condition with an else branch
11 /// **Why is this bad?** Negations reduce the readability of statements
13 /// **Known problems:** None
15 /// **Example:** if !v.is_empty() { a() } else { b() }
17 pub IF_NOT_ELSE, Allow,
18 "finds if branches that could be swapped so no negation operation is necessary on the condition"
23 impl LintPass for IfNotElse {
24 fn get_lints(&self) -> LintArray {
25 lint_array!(IF_NOT_ELSE)
29 impl EarlyLintPass for IfNotElse {
30 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
31 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
32 if let ExprKind::Block(..) = els.node {
34 ExprKind::Unary(UnOp::Not, _) => {
35 span_help_and_lint(cx,
38 "Unnecessary boolean `not` operation",
39 "remove the `!` and swap the blocks of the if/else");
41 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
42 span_help_and_lint(cx,
45 "Unnecessary `!=` operation",
46 "change to `==` and swap the blocks of the if/else");