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};
8 /// **What it does:** Checks for consecutive `if`s with the same condition.
10 /// **Why is this bad?** This is probably a copy & paste error.
12 /// **Known problems:** Hopefully none.
18 /// } else if a == b {
23 /// Note that this lint ignores all conditions with a function call as it could
24 /// have side effects:
29 /// } else if foo() { // not linted
35 "consecutive `if`s with the same condition"
38 declare_clippy_lint! {
39 /// **What it does:** Checks for consecutive `if`s with the same function call.
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".
45 /// **Known problems:** Hopefully none.
51 /// } else if foo() == bar {
56 /// This probably should be:
60 /// } else if foo() == baz {
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:
70 /// let first = foo();
74 /// let second = foo();
75 /// if second == bar {
80 pub SAME_FUNCTIONS_IN_IF_CONDITION,
82 "consecutive `if`s with the same function call"
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.
89 /// **Why is this bad?** This is probably a copy & paste error.
91 /// **Known problems:** Hopefully none.
101 pub IF_SAME_THEN_ELSE,
103 "`if` with the same `then` and `else` blocks"
106 declare_lint_pass!(CopyAndPaste => [IFS_SAME_COND, SAME_FUNCTIONS_IN_IF_CONDITION, IF_SAME_THEN_ELSE]);
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
113 kind: ExprKind::If(_, _, Some(ref else_expr)),
115 }) = get_parent_expr(cx, expr) {
116 if else_expr.hir_id == expr.hir_id {
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);
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) };
134 if let Some((i, j)) = search_same_sequenced(blocks, eq) {
139 "this `if` has identical blocks",
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);
154 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
156 for (i, j) in search_same(conds, hash, eq) {
161 "this `if` has the same condition as a previous `if`",
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);
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) {
181 // Do not spawn warning if `IFS_SAME_COND` already produced it.
182 if eq_expr_value(cx, lhs, rhs) {
185 SpanlessEq::new(cx).eq_expr(lhs, rhs)
188 for (i, j) in search_same(conds, hash, eq) {
191 SAME_FUNCTIONS_IN_IF_CONDITION,
193 "this `if` has the same function call as a previous `if`",
200 fn search_same_sequenced<T, Eq>(exprs: &[T], eq: Eq) -> Option<(&T, &T)>
202 Eq: Fn(&T, &T) -> bool,
204 for win in exprs.windows(2) {
205 if eq(&win[0], &win[1]) {
206 return Some((&win[0], &win[1]));