Skip to content

Commit a476af2

Browse files
committed
Correct error on partially unreachable or-pat in if let
1 parent 5c7bd52 commit a476af2

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

src/librustc_mir/hair/pattern/check_match.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,9 @@ fn check_arms<'p, 'tcx>(
414414
hir::MatchSource::IfDesugar { .. } | hir::MatchSource::WhileDesugar => {
415415
bug!()
416416
}
417-
hir::MatchSource::IfLetDesugar { .. } => {
418-
cx.tcx.lint_hir(
419-
lint::builtin::IRREFUTABLE_LET_PATTERNS,
420-
hir_pat.hir_id,
421-
pat.span,
422-
"irrefutable if-let pattern",
423-
);
424-
}
425417

426-
hir::MatchSource::WhileLetDesugar => {
418+
hir::MatchSource::IfLetDesugar { .. }
419+
| hir::MatchSource::WhileLetDesugar => {
427420
// check which arm we're on.
428421
match arm_index {
429422
// The arm with the user-specified pattern.
@@ -437,11 +430,20 @@ fn check_arms<'p, 'tcx>(
437430
}
438431
// The arm with the wildcard pattern.
439432
1 => {
433+
let msg = match source {
434+
hir::MatchSource::IfLetDesugar { .. } => {
435+
"irrefutable if-let pattern"
436+
}
437+
hir::MatchSource::WhileLetDesugar => {
438+
"irrefutable while-let pattern"
439+
}
440+
_ => bug!(),
441+
};
440442
cx.tcx.lint_hir(
441443
lint::builtin::IRREFUTABLE_LET_PATTERNS,
442444
hir_pat.hir_id,
443445
pat.span,
444-
"irrefutable while-let pattern",
446+
msg,
445447
);
446448
}
447449
_ => bug!(),

src/test/ui/pattern/usefulness/top-level-alternation.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
fn main() {
44
while let 0..=2 | 1 = 0 {} //~ ERROR unreachable pattern
5-
if let 0..=2 | 1 = 0 {} //~ WARN irrefutable if-let pattern
6-
// this one ^ is incorrect
5+
if let 0..=2 | 1 = 0 {} //~ ERROR unreachable pattern
76

87
match 0u8 {
98
0

src/test/ui/pattern/usefulness/top-level-alternation.stderr

+11-13
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,65 @@ note: lint level defined here
1010
LL | #![deny(unreachable_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

13-
warning: irrefutable if-let pattern
13+
error: unreachable pattern
1414
--> $DIR/top-level-alternation.rs:5:20
1515
|
1616
LL | if let 0..=2 | 1 = 0 {}
1717
| ^
18-
|
19-
= note: `#[warn(irrefutable_let_patterns)]` on by default
2018

2119
error: unreachable pattern
22-
--> $DIR/top-level-alternation.rs:10:15
20+
--> $DIR/top-level-alternation.rs:9:15
2321
|
2422
LL | | 0 => {}
2523
| ^
2624

2725
error: unreachable pattern
28-
--> $DIR/top-level-alternation.rs:15:15
26+
--> $DIR/top-level-alternation.rs:14:15
2927
|
3028
LL | | Some(0) => {}
3129
| ^^^^^^^
3230

3331
error: unreachable pattern
34-
--> $DIR/top-level-alternation.rs:20:9
32+
--> $DIR/top-level-alternation.rs:19:9
3533
|
3634
LL | (0, 0) => {}
3735
| ^^^^^^
3836

3937
error: unreachable pattern
40-
--> $DIR/top-level-alternation.rs:40:9
38+
--> $DIR/top-level-alternation.rs:39:9
4139
|
4240
LL | _ => {}
4341
| ^
4442

4543
error: unreachable pattern
46-
--> $DIR/top-level-alternation.rs:44:9
44+
--> $DIR/top-level-alternation.rs:43:9
4745
|
4846
LL | Some(_) => {}
4947
| ^^^^^^^
5048

5149
error: unreachable pattern
52-
--> $DIR/top-level-alternation.rs:45:9
50+
--> $DIR/top-level-alternation.rs:44:9
5351
|
5452
LL | None => {}
5553
| ^^^^
5654

5755
error: unreachable pattern
58-
--> $DIR/top-level-alternation.rs:50:9
56+
--> $DIR/top-level-alternation.rs:49:9
5957
|
6058
LL | None
6159
| ^^^^
6260

6361
error: unreachable pattern
64-
--> $DIR/top-level-alternation.rs:51:15
62+
--> $DIR/top-level-alternation.rs:50:15
6563
|
6664
LL | | Some(_) => {}
6765
| ^^^^^^^
6866

6967
error: unreachable pattern
70-
--> $DIR/top-level-alternation.rs:55:9
68+
--> $DIR/top-level-alternation.rs:54:9
7169
|
7270
LL | 1..=2 => {},
7371
| ^^^^^
7472

75-
error: aborting due to 10 previous errors
73+
error: aborting due to 11 previous errors
7674

0 commit comments

Comments
 (0)