Skip to content

Commit 9035708

Browse files
committed
When finding a match expr with a single arm that requires more, suggest it
Given ```rust match Some(42) { Some(0) => {} } ``` suggest ```rust match Some(42) { Some(0) => {} None | Some(_) => todo!(), } ```
1 parent e1458ee commit 9035708

31 files changed

+805
-159
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,21 @@ fn non_exhaustive_match<'p, 'tcx>(
591591
),
592592
));
593593
}
594+
[only] => {
595+
let pre_indentation = if let (Some(snippet), true) = (
596+
sm.indentation_before(only.span),
597+
sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
598+
) {
599+
format!("\n{}", snippet)
600+
} else {
601+
" ".to_string()
602+
};
603+
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
604+
suggestion = Some((
605+
only.span.shrink_to_hi(),
606+
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
607+
));
608+
}
594609
_ => {}
595610
}
596611

src/test/ui/closures/2229_closure_analysis/match/issue-88331.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ LL | move |i| match msg_type {
88
| ^^^^^^^^ patterns `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
99
|
1010
= note: the matched value is of type `Opcode`
11-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
11+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
12+
|
13+
LL ~ Opcode::OP1 => unimplemented!(),
14+
LL ~ Opcode(0_u8) | Opcode(2_u8..=u8::MAX) => todo!(),
15+
|
1216

1317
error[E0004]: non-exhaustive patterns: `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
1418
--> $DIR/issue-88331.rs:27:20
@@ -20,7 +24,11 @@ LL | move |i| match msg_type {
2024
| ^^^^^^^^ patterns `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
2125
|
2226
= note: the matched value is of type `Opcode2`
23-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
27+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
28+
|
29+
LL ~ Opcode2::OP2=> unimplemented!(),
30+
LL ~ Opcode2(Opcode(0_u8)) | Opcode2(Opcode(2_u8..=u8::MAX)) => todo!(),
31+
|
2432

2533
error: aborting due to 2 previous errors
2634

src/test/ui/closures/2229_closure_analysis/match/non-exhaustive-match.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ LL | let _b = || { match l1 { L1::A => () } };
1111
| ^^ pattern `B` not covered
1212
|
1313
= note: the matched value is of type `L1`
14-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
14+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
15+
|
16+
LL | let _b = || { match l1 { L1::A => (), B => todo!() } };
17+
| ++++++++++++++
1518

1619
error[E0004]: non-exhaustive patterns: type `E1` is non-empty
1720
--> $DIR/non-exhaustive-match.rs:37:25

src/test/ui/error-codes/E0004.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ LL | match x {
1212
| ^ pattern `HastaLaVistaBaby` not covered
1313
|
1414
= note: the matched value is of type `Terminator`
15-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
15+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
16+
|
17+
LL ~ Terminator::TalkToMyHand => {}
18+
LL + HastaLaVistaBaby => todo!()
19+
|
1620

1721
error: aborting due to previous error
1822

src/test/ui/feature-gates/feature-gate-precise_pointer_size_matching.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | match 0usize {
77
= note: the matched value is of type `usize`
88
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
99
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
10-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
10+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
11+
|
12+
LL ~ 0..=usize::MAX => {}
13+
LL + _ => todo!()
14+
|
1115

1216
error[E0004]: non-exhaustive patterns: `_` not covered
1317
--> $DIR/feature-gate-precise_pointer_size_matching.rs:10:11
@@ -18,7 +22,11 @@ LL | match 0isize {
1822
= note: the matched value is of type `isize`
1923
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
2024
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
21-
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
25+
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
26+
|
27+
LL ~ isize::MIN..=isize::MAX => {}
28+
LL + _ => todo!()
29+
|
2230

2331
error: aborting due to 2 previous errors
2432

0 commit comments

Comments
 (0)