1 //! lint on if branches that could be swapped so no `!` operation is necessary
7 use utils::{in_external_macro, span_help_and_lint};
9 /// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
12 /// **Why is this bad?** Negations reduce the readability of statements.
14 /// **Known problems:** None.
18 /// if !v.is_empty() {
37 "`if` branches that could be swapped so no negation operation is necessary on the condition"
42 impl LintPass for IfNotElse {
43 fn get_lints(&self) -> LintArray {
44 lint_array!(IF_NOT_ELSE)
48 impl EarlyLintPass for IfNotElse {
49 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
50 if in_external_macro(cx, item.span) {
53 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
54 if let ExprKind::Block(..) = els.node {
56 ExprKind::Unary(UnOp::Not, _) => {
61 "Unnecessary boolean `not` operation",
62 "remove the `!` and swap the blocks of the if/else",
65 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
70 "Unnecessary `!=` operation",
71 "change to `==` and swap the blocks of the if/else",