1 use crate::utils::{eq_expr_value, in_macro, search_same, SpanlessEq, SpanlessHash};
2 use crate::utils::{get_parent_expr, higher, if_sequence, span_lint_and_note};
3 use rustc_hir::{Block, Expr};
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
112 if let Some(expr) = get_parent_expr(cx, expr) {
113 if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) {
114 if else_expr.hir_id == expr.hir_id {
120 let (conds, blocks) = if_sequence(expr);
121 lint_same_then_else(cx, &blocks);
122 lint_same_cond(cx, &conds);
123 lint_same_fns_in_if_cond(cx, &conds);
128 /// Implementation of `IF_SAME_THEN_ELSE`.
129 fn lint_same_then_else(cx: &LateContext<'_>, blocks: &[&Block<'_>]) {
130 let eq: &dyn Fn(&&Block<'_>, &&Block<'_>) -> bool =
131 &|&lhs, &rhs| -> bool { SpanlessEq::new(cx).eq_block(lhs, rhs) };
133 if let Some((i, j)) = search_same_sequenced(blocks, eq) {
138 "this `if` has identical blocks",
145 /// Implementation of `IFS_SAME_COND`.
146 fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
147 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
148 let mut h = SpanlessHash::new(cx);
153 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
155 for (i, j) in search_same(conds, hash, eq) {
160 "this `if` has the same condition as a previous `if`",
167 /// Implementation of `SAME_FUNCTIONS_IN_IF_CONDITION`.
168 fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
169 let hash: &dyn Fn(&&Expr<'_>) -> u64 = &|expr| -> u64 {
170 let mut h = SpanlessHash::new(cx);
175 let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
176 // Do not lint if any expr originates from a macro
177 if in_macro(lhs.span) || in_macro(rhs.span) {
180 // Do not spawn warning if `IFS_SAME_COND` already produced it.
181 if eq_expr_value(cx, lhs, rhs) {
184 SpanlessEq::new(cx).eq_expr(lhs, rhs)
187 for (i, j) in search_same(conds, hash, eq) {
190 SAME_FUNCTIONS_IN_IF_CONDITION,
192 "this `if` has the same function call as a previous `if`",
199 fn search_same_sequenced<T, Eq>(exprs: &[T], eq: Eq) -> Option<(&T, &T)>
201 Eq: Fn(&T, &T) -> bool,
203 for win in exprs.windows(2) {
204 if eq(&win[0], &win[1]) {
205 return Some((&win[0], &win[1]));