Skip to content

Commit 59415b4

Browse files
committed
parent_node_is_if_expr now also recognizes if let as parent if
1 parent f7c2c44 commit 59415b4

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

clippy_lints/src/copies.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ fn lint_same_then_else<'tcx>(
290290
}
291291
}
292292

293+
/// The return tuple is structured as follows:
294+
/// 1. The amount of equal statements from the start
295+
/// 2. The amount of equal statements from the end
296+
/// 3. An indication if the block expressions are the same. This will also be true if both are `None`
297+
///
298+
/// This function can also trigger the `IF_SAME_THEN_ELSE` in which case it'll return `(0, 0, false)`
299+
/// to aboard any further processing and avoid duplicate lint triggers.
293300
fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize, usize, bool) {
294301
let mut start_eq = usize::MAX;
295302
let mut end_eq = usize::MAX;

clippy_utils/src/lib.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,13 +1195,29 @@ pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
11951195
let map = cx.tcx.hir();
11961196
let parent_id = map.get_parent_node(expr.hir_id);
11971197
let parent_node = map.get(parent_id);
1198-
matches!(
1199-
parent_node,
1200-
Node::Expr(Expr {
1201-
kind: ExprKind::If(_, _, _),
1202-
..
1203-
})
1204-
)
1198+
1199+
// Check for `if`
1200+
if_chain! {
1201+
if let Node::Expr(expr) = parent_node;
1202+
if let ExprKind::If(_, _, _) = expr.kind;
1203+
then {
1204+
return true;
1205+
}
1206+
}
1207+
1208+
// Check for `if let`
1209+
if_chain! {
1210+
if let Node::Arm(arm) = parent_node;
1211+
let arm_parent_id = map.get_parent_node(arm.hir_id);
1212+
let arm_parent_node = map.get(arm_parent_id);
1213+
if let Node::Expr(expr) = arm_parent_node;
1214+
if let ExprKind::Match(_, _, MatchSource::IfLetDesugar { .. }) = expr.kind;
1215+
then {
1216+
return true;
1217+
}
1218+
}
1219+
1220+
false
12051221
}
12061222

12071223
// Finds the `#[must_use]` attribute, if any

tests/ui/branches_sharing_code/shared_at_bottom.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,18 @@ fn fp_test() {
206206
}
207207
}
208208

209+
fn fp_if_let_issue7054() {
210+
// This shouldn't trigger the lint
211+
let string;
212+
let _x = if let true = true {
213+
""
214+
} else if true {
215+
string = "x".to_owned();
216+
&string
217+
} else {
218+
string = "y".to_owned();
219+
&string
220+
};
221+
}
222+
209223
fn main() {}

0 commit comments

Comments
 (0)