Skip to content

Commit d49229f

Browse files
committed
Don't suggest an arm when suggesting a never pattern
1 parent 6999acf commit d49229f

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,14 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10201020

10211021
let mut suggestion = None;
10221022
let sm = cx.tcx.sess.source_map();
1023+
let suggested_arm = if witnesses.len() < 4
1024+
&& witnesses.iter().all(|p| p.is_never_pattern())
1025+
&& cx.tcx.features().never_patterns
1026+
{
1027+
pattern
1028+
} else {
1029+
format!("{pattern} => todo!()")
1030+
};
10231031
match arms {
10241032
[] if sp.eq_ctxt(expr_span) => {
10251033
// Get the span for the empty match body `{}`.
@@ -1030,7 +1038,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10301038
};
10311039
suggestion = Some((
10321040
sp.shrink_to_hi().with_hi(expr_span.hi()),
1033-
format!(" {{{indentation}{more}{pattern} => todo!(),{indentation}}}",),
1041+
format!(" {{{indentation}{more}{suggested_arm},{indentation}}}",),
10341042
));
10351043
}
10361044
[only] => {
@@ -1056,7 +1064,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10561064
};
10571065
suggestion = Some((
10581066
only.span.shrink_to_hi(),
1059-
format!("{comma}{pre_indentation}{pattern} => todo!()"),
1067+
format!("{comma}{pre_indentation}{suggested_arm}"),
10601068
));
10611069
}
10621070
[.., prev, last] => {
@@ -1079,7 +1087,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10791087
if let Some(spacing) = spacing {
10801088
suggestion = Some((
10811089
last.span.shrink_to_hi(),
1082-
format!("{comma}{spacing}{pattern} => todo!()"),
1090+
format!("{comma}{spacing}{suggested_arm}"),
10831091
));
10841092
}
10851093
}

compiler/rustc_pattern_analysis/src/pat.rs

+8
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
321321
&self.ty
322322
}
323323

324+
pub fn is_never_pattern(&self) -> bool {
325+
match self.ctor() {
326+
Never => true,
327+
Or => self.fields.iter().all(|p| p.is_never_pattern()),
328+
_ => self.fields.iter().any(|p| p.is_never_pattern()),
329+
}
330+
}
331+
324332
pub fn iter_fields(&self) -> impl Iterator<Item = &WitnessPat<Cx>> {
325333
self.fields.iter()
326334
}

tests/ui/pattern/usefulness/empty-types.never_pats.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ note: `Result<u32, !>` defined here
111111
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
112112
|
113113
LL ~ Ok(_) => {},
114-
LL + Err(!) => todo!()
114+
LL + Err(!)
115115
|
116116

117117
error[E0004]: non-exhaustive patterns: `Ok(1_u32..=u32::MAX)` not covered
@@ -192,7 +192,7 @@ note: `Result<!, !>` defined here
192192
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
193193
|
194194
LL ~ match result_never {
195-
LL + Ok(!) | Err(!) => todo!(),
195+
LL + Ok(!) | Err(!),
196196
LL + }
197197
|
198198

@@ -210,8 +210,8 @@ note: `Result<!, !>` defined here
210210
= note: the matched value is of type `Result<!, !>`
211211
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
212212
|
213-
LL | Ok(_) => {}, Err(!) => todo!()
214-
| +++++++++++++++++++
213+
LL | Ok(_) => {}, Err(!)
214+
| ++++++++
215215

216216
error: unreachable pattern
217217
--> $DIR/empty-types.rs:140:13
@@ -240,7 +240,7 @@ note: `Option<Void>` defined here
240240
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
241241
|
242242
LL ~ None => {},
243-
LL + Some(!) => todo!()
243+
LL + Some(!)
244244
|
245245

246246
error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@@ -258,7 +258,7 @@ note: `Option<Void>` defined here
258258
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
259259
|
260260
LL ~ None => {},
261-
LL + Some(!) => todo!()
261+
LL + Some(!)
262262
|
263263

264264
error: unreachable pattern
@@ -343,7 +343,7 @@ note: `Result<!, !>` defined here
343343
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms
344344
|
345345
LL ~ match *x {
346-
LL + Ok(!) | Err(!) => todo!(),
346+
LL + Ok(!) | Err(!),
347347
LL ~ }
348348
|
349349

@@ -385,7 +385,7 @@ LL | match slice_never {
385385
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
386386
|
387387
LL ~ [] => {},
388-
LL + &[!, ..] => todo!()
388+
LL + &[!, ..]
389389
|
390390

391391
error[E0004]: non-exhaustive patterns: `&[]`, `&[!]` and `&[!, !]` not covered
@@ -492,7 +492,7 @@ note: `Option<!>` defined here
492492
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
493493
|
494494
LL ~ &None => {},
495-
LL + &Some(!) => todo!()
495+
LL + &Some(!)
496496
|
497497

498498
error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@@ -510,7 +510,7 @@ note: `Option<!>` defined here
510510
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
511511
|
512512
LL ~ None => {},
513-
LL + Some(!) => todo!()
513+
LL + Some(!)
514514
|
515515

516516
error[E0004]: non-exhaustive patterns: `Err(!)` not covered
@@ -528,7 +528,7 @@ note: `Result<!, !>` defined here
528528
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
529529
|
530530
LL ~ Ok(_) => {},
531-
LL + Err(!) => todo!()
531+
LL + Err(!)
532532
|
533533

534534
error[E0004]: non-exhaustive patterns: `Err(!)` not covered
@@ -546,7 +546,7 @@ note: `Result<!, !>` defined here
546546
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
547547
|
548548
LL ~ Ok(_a) => {},
549-
LL + Err(!) => todo!()
549+
LL + Err(!)
550550
|
551551

552552
error[E0004]: non-exhaustive patterns: type `(u32, !)` is non-empty
@@ -599,7 +599,7 @@ LL | match ref_never {
599599
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
600600
|
601601
LL ~ &_a if false => {},
602-
LL + &! => todo!()
602+
LL + &!
603603
|
604604

605605
error[E0004]: non-exhaustive patterns: `Ok(!)` not covered
@@ -617,7 +617,7 @@ note: `Result<!, !>` defined here
617617
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
618618
|
619619
LL ~ Err(_) => {},
620-
LL + Ok(!) => todo!()
620+
LL + Ok(!)
621621
|
622622

623623
error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@@ -635,7 +635,7 @@ note: `Option<Result<!, !>>` defined here
635635
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
636636
|
637637
LL ~ None => {},
638-
LL + Some(!) => todo!()
638+
LL + Some(!)
639639
|
640640

641641
error: aborting due to 49 previous errors; 1 warning emitted

tests/ui/rfcs/rfc-0000-never_patterns/check.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ note: `Option<Void>` defined here
4646
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
4747
|
4848
LL ~ None => {},
49-
LL + Some(!) => todo!()
49+
LL + Some(!)
5050
|
5151

5252
error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@@ -64,7 +64,7 @@ note: `Option<Void>` defined here
6464
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
6565
|
6666
LL ~ None => {},
67-
LL + Some(!) => todo!()
67+
LL + Some(!)
6868
|
6969

7070
error: aborting due to 6 previous errors

0 commit comments

Comments
 (0)