]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/copies.rs
Reintroduce hir::ExprKind::If
[rust.git] / clippy_lints / src / copies.rs
1 use crate::utils::{eq_expr_value, in_macro, search_same, SpanlessEq, SpanlessHash};
2 use crate::utils::{get_parent_expr, if_sequence, span_lint_and_note};
3 use rustc_hir::{Block, Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for consecutive `if`s with the same condition.
9     ///
10     /// **Why is this bad?** This is probably a copy & paste error.
11     ///
12     /// **Known problems:** Hopefully none.
13     ///
14     /// **Example:**
15     /// ```ignore
16     /// if a == b {
17     ///     …
18     /// } else if a == b {
19     ///     …
20     /// }
21     /// ```
22     ///
23     /// Note that this lint ignores all conditions with a function call as it could
24     /// have side effects:
25     ///
26     /// ```ignore
27     /// if foo() {
28     ///     …
29     /// } else if foo() { // not linted
30     ///     …
31     /// }
32     /// ```
33     pub IFS_SAME_COND,
34     correctness,
35     "consecutive `if`s with the same condition"
36 }
37
38 declare_clippy_lint! {
39     /// **What it does:** Checks for consecutive `if`s with the same function call.
40     ///
41     /// **Why is this bad?** This is probably a copy & paste error.
42     /// Despite the fact that function can have side effects and `if` works as
43     /// intended, such an approach is implicit and can be considered a "code smell".
44     ///
45     /// **Known problems:** Hopefully none.
46     ///
47     /// **Example:**
48     /// ```ignore
49     /// if foo() == bar {
50     ///     …
51     /// } else if foo() == bar {
52     ///     …
53     /// }
54     /// ```
55     ///
56     /// This probably should be:
57     /// ```ignore
58     /// if foo() == bar {
59     ///     …
60     /// } else if foo() == baz {
61     ///     …
62     /// }
63     /// ```
64     ///
65     /// or if the original code was not a typo and called function mutates a state,
66     /// consider move the mutation out of the `if` condition to avoid similarity to
67     /// a copy & paste error:
68     ///
69     /// ```ignore
70     /// let first = foo();
71     /// if first == bar {
72     ///     …
73     /// } else {
74     ///     let second = foo();
75     ///     if second == bar {
76     ///     …
77     ///     }
78     /// }
79     /// ```
80     pub SAME_FUNCTIONS_IN_IF_CONDITION,
81     pedantic,
82     "consecutive `if`s with the same function call"
83 }
84
85 declare_clippy_lint! {
86     /// **What it does:** Checks for `if/else` with the same body as the *then* part
87     /// and the *else* part.
88     ///
89     /// **Why is this bad?** This is probably a copy & paste error.
90     ///
91     /// **Known problems:** Hopefully none.
92     ///
93     /// **Example:**
94     /// ```ignore
95     /// let foo = if … {
96     ///     42
97     /// } else {
98     ///     42
99     /// };
100     /// ```
101     pub IF_SAME_THEN_ELSE,
102     correctness,
103     "`if` with the same `then` and `else` blocks"
104 }
105
106 declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, SAME_FUNCTIONS_IN_IF_CONDITION, IF_SAME_THEN_ELSE]);
107
108 impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
109     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
110         if !expr.span.from_expansion() {
111             // skip ifs directly in else, it will be checked in the parent if
112             if let Some(&Expr {
113                 kind: ExprKind::If(_, _, Some(ref else_expr)),
114                 ..
115             }) = get_parent_expr(cx, expr) {
116                 if else_expr.hir_id == expr.hir_id {
117                     return;
118                 }
119             }
120
121             let (conds, blocks) = if_sequence(expr);
122             lint_same_then_else(cx, &blocks);
123             lint_same_cond(cx, &conds);
124             lint_same_fns_in_if_cond(cx, &conds);
125         }
126     }
127 }
128
129 /// Implementation of `IF_SAME_THEN_ELSE`.
130 fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>]) {
131     let eq: &dyn Fn(&&Block<'_>, &&Block<'_>) -> bool =
132         &|&lhs, &rhs| -> bool { SpanlessEq::new(cx).eq_block(lhs, rhs) };
133
134     if let Some((i, j)) = search_same_sequenced(blocks, eq) {
135         span_lint_and_note(
136             cx,
137             IF_SAME_THEN_ELSE,
138             j.span,
139             "this `if` has identical blocks",
140             Some(i.span),
141             "same as this",
142         );
143     }
144 }
145
146 /// Implementation of `IFS_SAME_COND`.
147 fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
148     let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
149         let mut h = SpanlessHash::new(cx);
150         h.hash_expr(expr);
151         h.finish()
152     };
153
154     let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
155
156     for (i, j) in search_same(conds, hash, eq) {
157         span_lint_and_note(
158             cx,
159             IFS_SAME_COND,
160             j.span,
161             "this `if` has the same condition as a previous `if`",
162             Some(i.span),
163             "same as this",
164         );
165     }
166 }
167
168 /// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
169 fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
170     let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
171         let mut h = SpanlessHash::new(cx);
172         h.hash_expr(expr);
173         h.finish()
174     };
175
176     let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
177         // Do not lint if any expr originates from a macro
178         if in_macro(lhs.span) || in_macro(rhs.span) {
179             return false;
180         }
181         // Do not spawn warning if `IFS_SAME_COND` already produced it.
182         if eq_expr_value(cx, lhs, rhs) {
183             return false;
184         }
185         SpanlessEq::new(cx).eq_expr(lhs, rhs)
186     };
187
188     for (i, j) in search_same(conds, hash, eq) {
189         span_lint_and_note(
190             cx,
191             SAME_FUNCTIONS_IN_IF_CONDITION,
192             j.span,
193             "this `if` has the same function call as a previous `if`",
194             Some(i.span),
195             "same as this",
196         );
197     }
198 }
199
200 fn search_same_sequenced<T, Eq>(exprs: &[T], eq: Eq) -> Option<(&T, &T)>
201 where
202     Eq: Fn(&T, &T) -> bool,
203 {
204     for win in exprs.windows(2) {
205         if eq(&win[0], &win[1]) {
206             return Some((&win[0], &win[1]));
207         }
208     }
209     None
210 }