Fix usefulness check for never type

This commit is contained in:
Mikail Bagishov 2020-05-06 17:45:47 +03:00
parent efd8e34c39
commit 7c94fa7d01
No known key found for this signature in database
GPG key ID: 63CF4B1F9D921A40

View file

@ -573,14 +573,20 @@ pub(crate) fn is_useful(
matrix: &Matrix,
v: &PatStack,
) -> MatchCheckResult<Usefulness> {
// Handle the special case of enums with no variants. In that case, no match
// arm is useful.
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) =
cx.infer[cx.match_expr].strip_references()
{
if cx.db.enum_data(*enum_id).variants.is_empty() {
// Handle two special cases:
// - enum with no variants
// - `!` type
// In those cases, no match arm is useful.
match cx.infer[cx.match_expr].strip_references() {
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) => {
if cx.db.enum_data(*enum_id).variants.is_empty() {
return Ok(Usefulness::NotUseful);
}
}
Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => {
return Ok(Usefulness::NotUseful);
}
_ => (),
}
if v.is_empty() {
@ -1917,6 +1923,17 @@ mod tests {
check_no_diagnostic(content);
}
#[test]
fn type_never() {
let content = r"
fn test_fn(never: !) {
match never {}
}
";
check_no_diagnostic(content);
}
#[test]
fn enum_never_ref() {
let content = r"