]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/if_not_else.rs
Examine lifetimes in `OpaqueDef` bounds as well
[rust.git] / clippy_lints / src / if_not_else.rs
1 //! lint on if branches that could be swapped so no `!` operation is necessary
2 //! on the condition
3
4 use clippy_utils::diagnostics::span_lint_and_help;
5 use clippy_utils::is_else_clause;
6 use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9
10 declare_clippy_lint! {
11     /// ### What it does
12     /// Checks for usage of `!` or `!=` in an if condition with an
13     /// else branch.
14     ///
15     /// ### Why is this bad?
16     /// Negations reduce the readability of statements.
17     ///
18     /// ### Example
19     /// ```rust
20     /// # let v: Vec<usize> = vec![];
21     /// # fn a() {}
22     /// # fn b() {}
23     /// if !v.is_empty() {
24     ///     a()
25     /// } else {
26     ///     b()
27     /// }
28     /// ```
29     ///
30     /// Could be written:
31     ///
32     /// ```rust
33     /// # let v: Vec<usize> = vec![];
34     /// # fn a() {}
35     /// # fn b() {}
36     /// if v.is_empty() {
37     ///     b()
38     /// } else {
39     ///     a()
40     /// }
41     /// ```
42     pub IF_NOT_ELSE,
43     pedantic,
44     "`if` branches that could be swapped so no negation operation is necessary on the condition"
45 }
46
47 declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
48
49 impl LateLintPass<'_> for IfNotElse {
50     fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
51         // While loops will be desugared to ExprKind::If. This will cause the lint to fire.
52         // To fix this, return early if this span comes from a macro or desugaring.
53         if item.span.from_expansion() {
54             return;
55         }
56         if let ExprKind::If(cond, _, Some(els)) = item.kind {
57             if let ExprKind::Block(..) = els.kind {
58                 // Disable firing the lint in "else if" expressions.
59                 if is_else_clause(cx.tcx, item) {
60                     return;
61                 }
62
63                 match cond.peel_drop_temps().kind {
64                     ExprKind::Unary(UnOp::Not, _) => {
65                         span_lint_and_help(
66                             cx,
67                             IF_NOT_ELSE,
68                             item.span,
69                             "unnecessary boolean `not` operation",
70                             None,
71                             "remove the `!` and swap the blocks of the `if`/`else`",
72                         );
73                     },
74                     ExprKind::Binary(ref kind, _, _) if kind.node == BinOpKind::Ne => {
75                         span_lint_and_help(
76                             cx,
77                             IF_NOT_ELSE,
78                             item.span,
79                             "unnecessary `!=` operation",
80                             None,
81                             "change to `==` and swap the blocks of the `if`/`else`",
82                         );
83                     },
84                     _ => (),
85                 }
86             }
87         }
88     }
89 }