Skip to content

Commit 11d320a

Browse files
Not linting irrefutable_let_patterns on let chains
1 parent 9f2ef0f commit 11d320a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
569569
) {
570570
assert!(self.let_source != LetSource::None);
571571

572+
// for let chains, don't emit IRREFUTABLE_LET_PATTERNS
573+
if chain_refutabilities.len() > 1 {
574+
return;
575+
}
576+
572577
if chain_refutabilities.iter().all(|r| matches!(*r, Some((_, Irrefutable)))) {
573578
// The entire chain is made up of irrefutable `let` statements
574579
report_irrefutable_let_patterns(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ run-pass
2+
//@ edition:2024
3+
4+
#![allow(unused)]
5+
6+
enum Outer {
7+
Middle(Middle),
8+
}
9+
enum Middle {
10+
Inner(Inner),
11+
Other(bool),
12+
}
13+
struct Inner {
14+
id: u32,
15+
}
16+
17+
fn test_1(outer: Outer) {
18+
if let Outer::Middle(middle) = outer
19+
&& let Middle::Inner(internal) = middle
20+
&& let Inner { id } = internal
21+
{
22+
println!("{id}");
23+
}
24+
}
25+
26+
fn test_2() -> usize { 42 }
27+
28+
fn main() {
29+
if let mx = test_2() && mx < usize::MAX {
30+
println!("{mx}");
31+
} else if let x = test_2() && x > 0 {
32+
println!("{x}");
33+
}
34+
}

0 commit comments

Comments
 (0)