]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/if_not_else.rs
Merge pull request #3495 from flip1995/tykind_fix
[rust.git] / clippy_lints / src / if_not_else.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 //! lint on if branches that could be swapped so no `!` operation is necessary
11 //! on the condition
12
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::*;
16
17 use crate::utils::span_help_and_lint;
18
19 /// **What it does:** Checks for usage of `!` or `!=` in an if condition with an
20 /// else branch.
21 ///
22 /// **Why is this bad?** Negations reduce the readability of statements.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// if !v.is_empty() {
29 ///     a()
30 /// } else {
31 ///     b()
32 /// }
33 /// ```
34 ///
35 /// Could be written:
36 ///
37 /// ```rust
38 /// if v.is_empty() {
39 ///     b()
40 /// } else {
41 ///     a()
42 /// }
43 /// ```
44 declare_clippy_lint! {
45     pub IF_NOT_ELSE,
46     pedantic,
47     "`if` branches that could be swapped so no negation operation is necessary on the condition"
48 }
49
50 pub struct IfNotElse;
51
52 impl LintPass for IfNotElse {
53     fn get_lints(&self) -> LintArray {
54         lint_array!(IF_NOT_ELSE)
55     }
56 }
57
58 impl EarlyLintPass for IfNotElse {
59     fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
60         if in_external_macro(cx.sess(), item.span) {
61             return;
62         }
63         if let ExprKind::If(ref cond, _, Some(ref els)) = item.node {
64             if let ExprKind::Block(..) = els.node {
65                 match cond.node {
66                     ExprKind::Unary(UnOp::Not, _) => {
67                         span_help_and_lint(
68                             cx,
69                             IF_NOT_ELSE,
70                             item.span,
71                             "Unnecessary boolean `not` operation",
72                             "remove the `!` and swap the blocks of the if/else",
73                         );
74                     },
75                     ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
76                         span_help_and_lint(
77                             cx,
78                             IF_NOT_ELSE,
79                             item.span,
80                             "Unnecessary `!=` operation",
81                             "change to `==` and swap the blocks of the if/else",
82                         );
83                     },
84                     _ => (),
85                 }
86             }
87         }
88     }
89 }