Skip to content

Commit cbde4f2

Browse files
committed
parent_node_is_if_expr now also recognizes if let as parent if
1 parent b1c675f commit cbde4f2

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
@@ -1212,13 +1212,29 @@ pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
12121212
let map = cx.tcx.hir();
12131213
let parent_id = map.get_parent_node(expr.hir_id);
12141214
let parent_node = map.get(parent_id);
1215-
matches!(
1216-
parent_node,
1217-
Node::Expr(Expr {
1218-
kind: ExprKind::If(_, _, _),
1219-
..
1220-
})
1221-
)
1215+
1216+
// Check for `if`
1217+
if_chain! {
1218+
if let Node::Expr(expr) = parent_node;
1219+
if let ExprKind::If(_, _, _) = expr.kind;
1220+
then {
1221+
return true;
1222+
}
1223+
}
1224+
1225+
// Check for `if let`
1226+
if_chain! {
1227+
if let Node::Arm(arm) = parent_node;
1228+
let arm_parent_id = map.get_parent_node(arm.hir_id);
1229+
let arm_parent_node = map.get(arm_parent_id);
1230+
if let Node::Expr(expr) = arm_parent_node;
1231+
if let ExprKind::Match(_, _, MatchSource::IfLetDesugar { .. }) = expr.kind;
1232+
then {
1233+
return true;
1234+
}
1235+
}
1236+
1237+
false
12221238
}
12231239

12241240
// 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)