From 5fe608fb31430f43a404312e284a71d6f7cfa038 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 5 Apr 2020 12:42:24 -0700 Subject: handle match auto-deref --- crates/ra_hir_ty/src/_match.rs | 35 +++++++++++++++++++++++++++++++++++ crates/ra_hir_ty/src/expr.rs | 11 ++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_hir_ty/src/_match.rs b/crates/ra_hir_ty/src/_match.rs index 8b9bdb7cd..f502a9208 100644 --- a/crates/ra_hir_ty/src/_match.rs +++ b/crates/ra_hir_ty/src/_match.rs @@ -865,6 +865,41 @@ mod tests { check_no_diagnostic(content); } + #[test] + fn enum_ref_missing_arms() { + let content = r" + enum Either { + A, + B, + } + fn test_fn() { + match &Either::B { + Either::A => {}, + } + } + "; + + check_diagnostic_with_no_fix(content); + } + + #[test] + fn enum_ref_no_diagnostic() { + let content = r" + enum Either { + A, + B, + } + fn test_fn() { + match &Either::B { + Either::A => {}, + Either::B => {}, + } + } + "; + + check_no_diagnostic(content); + } + #[test] fn enum_containing_bool_no_arms() { let content = r" diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs index 8c7d1a98e..6efed6f9e 100644 --- a/crates/ra_hir_ty/src/expr.rs +++ b/crates/ra_hir_ty/src/expr.rs @@ -93,7 +93,16 @@ impl<'a, 'b> ExprValidator<'a, 'b> { // of the match expression. If we had a InvalidMatchArmPattern // diagnostic or similar we could raise that in an else // block here. - if pat_ty == match_expr_ty { + // + // When comparing the types, we also have to consider that rustc + // will automatically de-reference the match expression type if + // necessary. + if pat_ty == match_expr_ty + || match_expr_ty + .as_reference() + .map(|(match_expr_ty, _)| match_expr_ty == pat_ty) + .unwrap_or(false) + { // If we had a NotUsefulMatchArm diagnostic, we could // check the usefulness of each pattern as we added it // to the matrix here. -- cgit v1.2.3