5787: Fix missing match arm false positive r=matklad a=CAD97

If the type of the match expression is `{unknown}`, don't do exhaustiveness checks, as it could be an uninhabited type.

Co-authored-by: CAD97 <cad97@cad97.com>
This commit is contained in:
bors[bot] 2020-08-18 07:25:57 +00:00 committed by GitHub
commit 3932874794
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -223,10 +223,10 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
db.body_with_source_map(self.owner.into()); db.body_with_source_map(self.owner.into());
let match_expr_ty = match infer.type_of_expr.get(match_expr) { let match_expr_ty = match infer.type_of_expr.get(match_expr) {
Some(ty) => ty,
// If we can't resolve the type of the match expression // If we can't resolve the type of the match expression
// we cannot perform exhaustiveness checks. // we cannot perform exhaustiveness checks.
None => return, None | Some(Ty::Unknown) => return,
Some(ty) => ty,
}; };
let cx = MatchCheckCtx { match_expr, body, infer: infer.clone(), db }; let cx = MatchCheckCtx { match_expr, body, infer: infer.clone(), db };

View file

@ -1335,6 +1335,23 @@ fn panic(a: Category, b: Category) {
); );
} }
#[test]
fn unknown_type() {
check_diagnostics(
r#"
enum Option<T> { Some(T), None }
fn main() {
// `Never` is deliberately not defined so that it's an uninferred type.
match Option::<Never>::None {
None => (),
Some(never) => match never {},
}
}
"#,
);
}
mod false_negatives { mod false_negatives {
//! The implementation of match checking here is a work in progress. As we roll this out, we //! The implementation of match checking here is a work in progress. As we roll this out, we
//! prefer false negatives to false positives (ideally there would be no false positives). This //! prefer false negatives to false positives (ideally there would be no false positives). This