Skip to content

Commit c0599aa

Browse files
committed
Fix needless_match FP on if-lets
1 parent 1bdc08a commit c0599aa

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

clippy_lints/src/matches/needless_match.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ fn check_if_let_inner(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool
9191

9292
// Recursively check for each `else if let` phrase,
9393
if let Some(ref nested_if_let) = higher::IfLet::hir(cx, if_else) {
94+
if let ExprKind::Call(
95+
Expr {
96+
kind: ExprKind::Path(QPath::Resolved(_, Path { res: outer_res, .. })),
97+
..
98+
},
99+
..,
100+
) = if_let.let_expr.kind
101+
&& let ExprKind::Call(
102+
Expr {
103+
kind: ExprKind::Path(QPath::Resolved(_, Path { res: nested_res, .. })),
104+
..
105+
},
106+
..,
107+
) = nested_if_let.let_expr.kind
108+
&& outer_res != nested_res
109+
{
110+
return false;
111+
}
94112
return check_if_let_inner(cx, nested_if_let);
95113
}
96114

tests/ui/needless_match.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,29 @@ mod issue9084 {
245245
}
246246
}
247247

248+
fn a() -> Option<()> {
249+
Some(())
250+
}
251+
fn b() -> Option<()> {
252+
Some(())
253+
}
254+
fn c() -> Option<()> {
255+
Some(())
256+
}
257+
258+
#[allow(clippy::ifs_same_cond)]
259+
pub fn issue13574() -> Option<()> {
260+
// Do not lint.
261+
// The right hand of all these arms are different functions.
262+
if let Some(a) = a() {
263+
Some(a)
264+
} else if let Some(b) = b() {
265+
Some(b)
266+
} else if let Some(c) = c() {
267+
Some(c)
268+
} else {
269+
None
270+
}
271+
}
272+
248273
fn main() {}

tests/ui/needless_match.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,29 @@ mod issue9084 {
289289
}
290290
}
291291

292+
fn a() -> Option<()> {
293+
Some(())
294+
}
295+
fn b() -> Option<()> {
296+
Some(())
297+
}
298+
fn c() -> Option<()> {
299+
Some(())
300+
}
301+
302+
#[allow(clippy::ifs_same_cond)]
303+
pub fn issue13574() -> Option<()> {
304+
// Do not lint.
305+
// The right hand of all these arms are different functions.
306+
if let Some(a) = a() {
307+
Some(a)
308+
} else if let Some(b) = b() {
309+
Some(b)
310+
} else if let Some(c) = c() {
311+
Some(c)
312+
} else {
313+
None
314+
}
315+
}
316+
292317
fn main() {}

0 commit comments

Comments
 (0)