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:** Checks for usage of `!` or `!=` in an if condition with an
11 /// **Why is this bad?** Negations reduce the readability of statements.
13 /// **Known problems:** None.
17 /// if !v.is_empty() {
36 "`if` branches that could be swapped so no negation operation is necessary on the condition"
41 impl LintPass for IfNotElse {
42 fn get_lints(&self) -> LintArray {
43 lint_array!(IF_NOT_ELSE)
47 impl EarlyLintPass for IfNotElse {
48 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
49 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
50 if let ExprKind::Block(..) = els.node {
52 ExprKind::Unary(UnOp::Not, _) => {
53 span_help_and_lint(cx,
56 "Unnecessary boolean `not` operation",
57 "remove the `!` and swap the blocks of the if/else");
59 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
60 span_help_and_lint(cx,
63 "Unnecessary `!=` operation",
64 "change to `==` and swap the blocks of the if/else");