1 use crate::utils::{eq_expr_value, in_macro, search_same, SpanlessEq, SpanlessHash};
2 use crate::utils::{get_parent_expr, if_sequence};
3 use clippy_utils::diagnostics::span_lint_and_note;
4 use rustc_hir::{Block, Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for consecutive `if`s with the same condition.
11 /// **Why is this bad?** This is probably a copy & paste error.
13 /// **Known problems:** Hopefully none.
19 /// } else if a == b {
24 /// Note that this lint ignores all conditions with a function call as it could
25 /// have side effects:
30 /// } else if foo() { // not linted
36 "consecutive `if`s with the same condition"
39 declare_clippy_lint! {
40 /// **What it does:** Checks for consecutive `if`s with the same function call.
42 /// **Why is this bad?** This is probably a copy & paste error.
43 /// Despite the fact that function can have side effects and `if` works as
44 /// intended, such an approach is implicit and can be considered a "code smell".
46 /// **Known problems:** Hopefully none.
52 /// } else if foo() == bar {
57 /// This probably should be:
61 /// } else if foo() == baz {
66 /// or if the original code was not a typo and called function mutates a state,
67 /// consider move the mutation out of the `if` condition to avoid similarity to
68 /// a copy & paste error:
71 /// let first = foo();
75 /// let second = foo();
76 /// if second == bar {
81 pub SAME_FUNCTIONS_IN_IF_CONDITION,
83 "consecutive `if`s with the same function call"
86 declare_clippy_lint! {
87 /// **What it does:** Checks for `if/else` with the same body as the *then* part
88 /// and the *else* part.
90 /// **Why is this bad?** This is probably a copy & paste error.
92 /// **Known problems:** Hopefully none.
102 pub IF_SAME_THEN_ELSE,
104 "`if` with the same `then` and `else` blocks"
107 declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, SAME_FUNCTIONS_IN_IF_CONDITION, IF_SAME_THEN_ELSE]);
109 impl<'tcx> LateLintPass<'tcx> for CopyAndPaste {
110 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
111 if !expr.span.from_expansion() {
112 // skip ifs directly in else, it will be checked in the parent if
114 kind: ExprKind::If(_, _, Some(ref else_expr)),
116 }) = get_parent_expr(cx, expr)
118 if else_expr.hir_id == expr.hir_id {
123 let (conds, blocks) = if_sequence(expr);
124 lint_same_then_else(cx, &blocks);
125 lint_same_cond(cx, &conds);
126 lint_same_fns_in_if_cond(cx, &conds);
131 /// Implementation of `IF_SAME_THEN_ELSE`.
132 fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>]) {
133 let eq: &dyn Fn(&&Block<'_>, &&Block<'_>) -> bool =
134 &|&lhs, &rhs| -> bool { SpanlessEq::new(cx).eq_block(lhs, rhs) };
136 if let Some((i, j)) = search_same_sequenced(blocks, eq) {
141 "this `if` has identical blocks",
148 /// Implementation of `IFS_SAME_COND`.
149 fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
150 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
151 let mut h = SpanlessHash::new(cx);
156 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
158 for (i, j) in search_same(conds, hash, eq) {
163 "this `if` has the same condition as a previous `if`",
170 /// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
171 fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
172 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
173 let mut h = SpanlessHash::new(cx);
178 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
179 // Do not lint if any expr originates from a macro
180 if in_macro(lhs.span) || in_macro(rhs.span) {
183 // Do not spawn warning if `IFS_SAME_COND` already produced it.
184 if eq_expr_value(cx, lhs, rhs) {
187 SpanlessEq::new(cx).eq_expr(lhs, rhs)
190 for (i, j) in search_same(conds, hash, eq) {
193 SAME_FUNCTIONS_IN_IF_CONDITION,
195 "this `if` has the same function call as a previous `if`",
202 fn search_same_sequenced<T, Eq>(exprs: &[T], eq: Eq) -> Option<(&T, &T)>
204 Eq: Fn(&T, &T) -> bool,
206 for win in exprs.windows(2) {
207 if eq(&win[0], &win[1]) {
208 return Some((&win[0], &win[1]));