]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/copies.rs
Auto merge of #6555 - stanislav-tkach:patch-1, r=flip1995
[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             {
117                 if else_expr.hir_id == expr.hir_id {
118                     return;
119                 }
120             }
121
122             let (conds, blocks) = if_sequence(expr);
123             lint_same_then_else(cx, &blocks);
124             lint_same_cond(cx, &conds);
125             lint_same_fns_in_if_cond(cx, &conds);
126         }
127     }
128 }
129
130 /// Implementation of `IF_SAME_THEN_ELSE`.
131 fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>]) {
132     let eq: &dyn Fn(&&Block<'_>, &&Block<'_>) -> bool =
133         &|&lhs, &rhs| -> bool { SpanlessEq::new(cx).eq_block(lhs, rhs) };
134
135     if let Some((i, j)) = search_same_sequenced(blocks, eq) {
136         span_lint_and_note(
137             cx,
138             IF_SAME_THEN_ELSE,
139             j.span,
140             "this `if` has identical blocks",
141             Some(i.span),
142             "same as this",
143         );
144     }
145 }
146
147 /// Implementation of `IFS_SAME_COND`.
148 fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
149     let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
150         let mut h = SpanlessHash::new(cx);
151         h.hash_expr(expr);
152         h.finish()
153     };
154
155     let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
156
157     for (i, j) in search_same(conds, hash, eq) {
158         span_lint_and_note(
159             cx,
160             IFS_SAME_COND,
161             j.span,
162             "this `if` has the same condition as a previous `if`",
163             Some(i.span),
164             "same as this",
165         );
166     }
167 }
168
169 /// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
170 fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
171     let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
172         let mut h = SpanlessHash::new(cx);
173         h.hash_expr(expr);
174         h.finish()
175     };
176
177     let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
178         // Do not lint if any expr originates from a macro
179         if in_macro(lhs.span) || in_macro(rhs.span) {
180             return false;
181         }
182         // Do not spawn warning if `IFS_SAME_COND` already produced it.
183         if eq_expr_value(cx, lhs, rhs) {
184             return false;
185         }
186         SpanlessEq::new(cx).eq_expr(lhs, rhs)
187     };
188
189     for (i, j) in search_same(conds, hash, eq) {
190         span_lint_and_note(
191             cx,
192             SAME_FUNCTIONS_IN_IF_CONDITION,
193             j.span,
194             "this `if` has the same function call as a previous `if`",
195             Some(i.span),
196             "same as this",
197         );
198     }
199 }
200
201 fn search_same_sequenced<T, Eq>(exprs: &[T], eq: Eq) -> Option<(&T, &T)>
202 where
203     Eq: Fn(&T, &T) -> bool,
204 {
205     for win in exprs.windows(2) {
206         if eq(&win[0], &win[1]) {
207             return Some((&win[0], &win[1]));
208         }
209     }
210     None
211 }