1 //! lint on if branches that could be swapped so no `!` operation is necessary
5 use rustc::{declare_lint, lint_array};
8 use crate::utils::{in_external_macro, span_help_and_lint};
10 /// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
13 /// **Why is this bad?** Negations reduce the readability of statements.
15 /// **Known problems:** None.
19 /// if !v.is_empty() {
35 declare_clippy_lint! {
38 "`if` branches that could be swapped so no negation operation is necessary on the condition"
43 impl LintPass for IfNotElse {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(IF_NOT_ELSE)
49 impl EarlyLintPass for IfNotElse {
50 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51 if in_external_macro(cx, item.span) {
54 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
55 if let ExprKind::Block(..) = els.node {
57 ExprKind::Unary(UnOp::Not, _) => {
62 "Unnecessary boolean `not` operation",
63 "remove the `!` and swap the blocks of the if/else",
66 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
71 "Unnecessary `!=` operation",
72 "change to `==` and swap the blocks of the if/else",