1 //! lint on if branches that could be swapped so no `!` operation is necessary on the condition
6 use utils::span_help_and_lint;
8 /// **What it does:** Warns on the use of `!` or `!=` in an if condition with an else branch
10 /// **Why is this bad?** Negations reduce the readability of statements
12 /// **Known problems:** None
16 /// if !v.is_empty() {
33 pub IF_NOT_ELSE, Allow,
34 "finds if branches that could be swapped so no negation operation is necessary on the condition"
39 impl LintPass for IfNotElse {
40 fn get_lints(&self) -> LintArray {
41 lint_array!(IF_NOT_ELSE)
45 impl EarlyLintPass for IfNotElse {
46 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
47 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
48 if let ExprKind::Block(..) = els.node {
50 ExprKind::Unary(UnOp::Not, _) => {
51 span_help_and_lint(cx,
54 "Unnecessary boolean `not` operation",
55 "remove the `!` and swap the blocks of the if/else");
57 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
58 span_help_and_lint(cx,
61 "Unnecessary `!=` operation",
62 "change to `==` and swap the blocks of the if/else");