1
Fork 0

Don't suggest an arm when suggesting a never pattern

This commit is contained in:
Nadrieril 2024-02-28 22:26:20 +01:00
parent 9f2aa5b85a
commit b878ab6a27
4 changed files with 36 additions and 20 deletions

View file

@ -1024,6 +1024,14 @@ fn report_non_exhaustive_match<'p, 'tcx>(
let mut suggestion = None; let mut suggestion = None;
let sm = cx.tcx.sess.source_map(); let sm = cx.tcx.sess.source_map();
let suggested_arm = if witnesses.len() < 4
&& witnesses.iter().all(|p| p.is_never_pattern())
&& cx.tcx.features().never_patterns
{
pattern
} else {
format!("{pattern} => todo!()")
};
match arms { match arms {
[] if sp.eq_ctxt(expr_span) => { [] if sp.eq_ctxt(expr_span) => {
// Get the span for the empty match body `{}`. // Get the span for the empty match body `{}`.
@ -1034,7 +1042,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
}; };
suggestion = Some(( suggestion = Some((
sp.shrink_to_hi().with_hi(expr_span.hi()), sp.shrink_to_hi().with_hi(expr_span.hi()),
format!(" {{{indentation}{more}{pattern} => todo!(),{indentation}}}",), format!(" {{{indentation}{more}{suggested_arm},{indentation}}}",),
)); ));
} }
[only] => { [only] => {
@ -1060,7 +1068,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
}; };
suggestion = Some(( suggestion = Some((
only.span.shrink_to_hi(), only.span.shrink_to_hi(),
format!("{comma}{pre_indentation}{pattern} => todo!()"), format!("{comma}{pre_indentation}{suggested_arm}"),
)); ));
} }
[.., prev, last] => { [.., prev, last] => {
@ -1083,7 +1091,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
if let Some(spacing) = spacing { if let Some(spacing) = spacing {
suggestion = Some(( suggestion = Some((
last.span.shrink_to_hi(), last.span.shrink_to_hi(),
format!("{comma}{spacing}{pattern} => todo!()"), format!("{comma}{spacing}{suggested_arm}"),
)); ));
} }
} }

View file

@ -321,6 +321,14 @@ impl<Cx: TypeCx> WitnessPat<Cx> {
&self.ty &self.ty
} }
pub fn is_never_pattern(&self) -> bool {
match self.ctor() {
Never => true,
Or => self.fields.iter().all(|p| p.is_never_pattern()),
_ => self.fields.iter().any(|p| p.is_never_pattern()),
}
}
pub fn iter_fields(&self) -> impl Iterator<Item = &WitnessPat<Cx>> { pub fn iter_fields(&self) -> impl Iterator<Item = &WitnessPat<Cx>> {
self.fields.iter() self.fields.iter()
} }

View file

@ -111,7 +111,7 @@ note: `Result<u32, !>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ Ok(_) => {}, LL ~ Ok(_) => {},
LL + Err(!) => todo!() LL + Err(!)
| |
error[E0004]: non-exhaustive patterns: `Ok(1_u32..=u32::MAX)` not covered error[E0004]: non-exhaustive patterns: `Ok(1_u32..=u32::MAX)` not covered
@ -192,7 +192,7 @@ note: `Result<!, !>` defined here
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 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
| |
LL ~ match result_never { LL ~ match result_never {
LL + Ok(!) | Err(!) => todo!(), LL + Ok(!) | Err(!),
LL + } LL + }
| |
@ -210,8 +210,8 @@ note: `Result<!, !>` defined here
= note: the matched value is of type `Result<!, !>` = note: the matched value is of type `Result<!, !>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL | Ok(_) => {}, Err(!) => todo!() LL | Ok(_) => {}, Err(!)
| +++++++++++++++++++ | ++++++++
error: unreachable pattern error: unreachable pattern
--> $DIR/empty-types.rs:140:13 --> $DIR/empty-types.rs:140:13
@ -240,7 +240,7 @@ note: `Option<Void>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ None => {}, LL ~ None => {},
LL + Some(!) => todo!() LL + Some(!)
| |
error[E0004]: non-exhaustive patterns: `Some(!)` not covered error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@ -258,7 +258,7 @@ note: `Option<Void>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ None => {}, LL ~ None => {},
LL + Some(!) => todo!() LL + Some(!)
| |
error: unreachable pattern error: unreachable pattern
@ -343,7 +343,7 @@ note: `Result<!, !>` defined here
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 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
| |
LL ~ match *x { LL ~ match *x {
LL + Ok(!) | Err(!) => todo!(), LL + Ok(!) | Err(!),
LL ~ } LL ~ }
| |
@ -385,7 +385,7 @@ LL | match slice_never {
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ [] => {}, LL ~ [] => {},
LL + &[!, ..] => todo!() LL + &[!, ..]
| |
error[E0004]: non-exhaustive patterns: `&[]`, `&[!]` and `&[!, !]` not covered error[E0004]: non-exhaustive patterns: `&[]`, `&[!]` and `&[!, !]` not covered
@ -492,7 +492,7 @@ note: `Option<!>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ &None => {}, LL ~ &None => {},
LL + &Some(!) => todo!() LL + &Some(!)
| |
error[E0004]: non-exhaustive patterns: `Some(!)` not covered error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@ -510,7 +510,7 @@ note: `Option<!>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ None => {}, LL ~ None => {},
LL + Some(!) => todo!() LL + Some(!)
| |
error[E0004]: non-exhaustive patterns: `Err(!)` not covered error[E0004]: non-exhaustive patterns: `Err(!)` not covered
@ -528,7 +528,7 @@ note: `Result<!, !>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ Ok(_) => {}, LL ~ Ok(_) => {},
LL + Err(!) => todo!() LL + Err(!)
| |
error[E0004]: non-exhaustive patterns: `Err(!)` not covered error[E0004]: non-exhaustive patterns: `Err(!)` not covered
@ -546,7 +546,7 @@ note: `Result<!, !>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ Ok(_a) => {}, LL ~ Ok(_a) => {},
LL + Err(!) => todo!() LL + Err(!)
| |
error[E0004]: non-exhaustive patterns: type `(u32, !)` is non-empty error[E0004]: non-exhaustive patterns: type `(u32, !)` is non-empty
@ -599,7 +599,7 @@ LL | match ref_never {
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ &_a if false => {}, LL ~ &_a if false => {},
LL + &! => todo!() LL + &!
| |
error[E0004]: non-exhaustive patterns: `Ok(!)` not covered error[E0004]: non-exhaustive patterns: `Ok(!)` not covered
@ -617,7 +617,7 @@ note: `Result<!, !>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ Err(_) => {}, LL ~ Err(_) => {},
LL + Ok(!) => todo!() LL + Ok(!)
| |
error[E0004]: non-exhaustive patterns: `Some(!)` not covered error[E0004]: non-exhaustive patterns: `Some(!)` not covered
@ -635,7 +635,7 @@ note: `Option<Result<!, !>>` defined here
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
| |
LL ~ None => {}, LL ~ None => {},
LL + Some(!) => todo!() LL + Some(!)
| |
error: aborting due to 49 previous errors; 1 warning emitted error: aborting due to 49 previous errors; 1 warning emitted

View file

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