1 //! lint on if branches that could be swapped so no `!` operation is necessary
4 use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
8 use crate::utils::span_help_and_lint;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
14 /// **Why is this bad?** Negations reduce the readability of statements.
16 /// **Known problems:** None.
20 /// # let v: Vec<usize> = vec![];
23 /// if !v.is_empty() {
33 /// # let v: Vec<usize> = vec![];
44 "`if` branches that could be swapped so no negation operation is necessary on the condition"
47 declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
49 impl EarlyLintPass for IfNotElse {
50 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51 if in_external_macro(cx.sess(), item.span) {
54 if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
55 if let ExprKind::Block(..) = els.kind {
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",