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_tool_lint, lint_array};
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 /// if !v.is_empty() {
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)
48 fn name(&self) -> &'static str {
53 impl EarlyLintPass for IfNotElse {
54 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
55 if in_external_macro(cx.sess(), item.span) {
58 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
59 if let ExprKind::Block(..) = els.node {
61 ExprKind::Unary(UnOp::Not, _) => {
66 "Unnecessary boolean `not` operation",
67 "remove the `!` and swap the blocks of the if/else",
70 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
75 "Unnecessary `!=` operation",
76 "change to `==` and swap the blocks of the if/else",