Skip to content

Commit d0fa808

Browse files
committed
Fix if_same_then_else with if let conditions
Apparently they are no longer split 1 LHS + rest RHS
1 parent 31baffd commit d0fa808

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

clippy_lints/src/copies.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_note, span_lint_and_then};
3+
use clippy_utils::higher::has_let_expr;
34
use clippy_utils::source::{IntoSpan, SpanRangeExt, first_line_of_span, indent_of, reindent_multiline, snippet};
45
use clippy_utils::ty::{InteriorMut, needs_ordered_drop};
56
use clippy_utils::visitors::for_each_expr_without_closures;
@@ -11,7 +12,7 @@ use clippy_utils::{
1112
use core::iter;
1213
use core::ops::ControlFlow;
1314
use rustc_errors::Applicability;
14-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, HirIdSet, LetStmt, Node, Stmt, StmtKind, intravisit};
15+
use rustc_hir::{Block, Expr, ExprKind, HirId, HirIdSet, LetStmt, Node, Stmt, StmtKind, intravisit};
1516
use rustc_lint::{LateContext, LateLintPass};
1617
use rustc_middle::ty::TyCtxt;
1718
use rustc_session::impl_lint_pass;
@@ -189,24 +190,13 @@ impl<'tcx> LateLintPass<'tcx> for CopyAndPaste<'tcx> {
189190
}
190191
}
191192

192-
/// Checks if the given expression is a let chain.
193-
fn contains_let(e: &Expr<'_>) -> bool {
194-
match e.kind {
195-
ExprKind::Let(..) => true,
196-
ExprKind::Binary(op, lhs, rhs) if op.node == BinOpKind::And => {
197-
matches!(lhs.kind, ExprKind::Let(..)) || contains_let(rhs)
198-
},
199-
_ => false,
200-
}
201-
}
202-
203193
fn lint_if_same_then_else(cx: &LateContext<'_>, conds: &[&Expr<'_>], blocks: &[&Block<'_>]) -> bool {
204194
let mut eq = SpanlessEq::new(cx);
205195
blocks
206196
.array_windows::<2>()
207197
.enumerate()
208198
.fold(true, |all_eq, (i, &[lhs, rhs])| {
209-
if eq.eq_block(lhs, rhs) && !contains_let(conds[i]) && conds.get(i + 1).is_none_or(|e| !contains_let(e)) {
199+
if eq.eq_block(lhs, rhs) && !has_let_expr(conds[i]) && conds.get(i + 1).is_none_or(|e| !has_let_expr(e)) {
210200
span_lint_and_note(
211201
cx,
212202
IF_SAME_THEN_ELSE,

0 commit comments

Comments
 (0)