1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on if branches that could be swapped so no `!` operation is necessary
13 use crate::rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::ast::*;
17 use crate::utils::span_help_and_lint;
19 /// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
22 /// **Why is this bad?** Negations reduce the readability of statements.
24 /// **Known problems:** None.
28 /// if !v.is_empty() {
44 declare_clippy_lint! {
47 "`if` branches that could be swapped so no negation operation is necessary on the condition"
52 impl LintPass for IfNotElse {
53 fn get_lints(&self) -> LintArray {
54 lint_array!(IF_NOT_ELSE)
58 impl EarlyLintPass for IfNotElse {
59 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
60 if in_external_macro(cx.sess(), item.span) {
63 if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
64 if let ExprKind::Block(..) = els.node {
66 ExprKind::Unary(UnOp::Not, _) => {
71 "Unnecessary boolean `not` operation",
72 "remove the `!` and swap the blocks of the if/else",
75 ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
80 "Unnecessary `!=` operation",
81 "change to `==` and swap the blocks of the if/else",