From c38aa5f4f0155ca484e954c179e94af831e7878b Mon Sep 17 00:00:00 2001 From: iDawer Date: Fri, 10 Mar 2023 16:16:28 +0500 Subject: [PATCH 1/4] Use anonymous lifetimes --- crates/hir-ty/src/diagnostics/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs index 91b46702893..4148d39a9d5 100644 --- a/crates/hir-ty/src/diagnostics/expr.rs +++ b/crates/hir-ty/src/diagnostics/expr.rs @@ -379,7 +379,7 @@ fn missing_match_arms<'p>( arms: &[MatchArm], ) -> String { struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>); - impl<'a, 'p> fmt::Display for DisplayWitness<'a, 'p> { + impl fmt::Display for DisplayWitness<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let DisplayWitness(witness, cx) = *self; let pat = witness.to_pat(cx); From 8f189f62c671aab20f16f1ed5586351556ffe8bd Mon Sep 17 00:00:00 2001 From: iDawer Date: Fri, 10 Mar 2023 16:35:04 +0500 Subject: [PATCH 2/4] Remove unnecessary argument --- crates/hir-ty/src/diagnostics/expr.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs index 4148d39a9d5..c728719997c 100644 --- a/crates/hir-ty/src/diagnostics/expr.rs +++ b/crates/hir-ty/src/diagnostics/expr.rs @@ -84,7 +84,7 @@ impl ExprValidator { match expr { Expr::Match { expr, arms } => { - self.validate_match(id, *expr, arms, db, self.infer.clone()); + self.validate_match(id, *expr, arms, db); } Expr::Call { .. } | Expr::MethodCall { .. } => { self.validate_call(db, id, expr, &mut filter_map_next_checker); @@ -151,11 +151,10 @@ impl ExprValidator { match_expr: ExprId, arms: &[MatchArm], db: &dyn HirDatabase, - infer: Arc, ) { let body = db.body(self.owner); - let match_expr_ty = &infer[match_expr]; + let match_expr_ty = &self.infer[match_expr]; if match_expr_ty.is_unknown() { return; } @@ -166,7 +165,7 @@ impl ExprValidator { let mut m_arms = Vec::with_capacity(arms.len()); let mut has_lowering_errors = false; for arm in arms { - if let Some(pat_ty) = infer.type_of_pat.get(arm.pat) { + if let Some(pat_ty) = self.infer.type_of_pat.get(arm.pat) { // We only include patterns whose type matches the type // of the match expression. If we had an InvalidMatchArmPattern // diagnostic or similar we could raise that in an else @@ -182,7 +181,7 @@ impl ExprValidator { .as_reference() .map(|(match_expr_ty, ..)| match_expr_ty == pat_ty) .unwrap_or(false)) - && types_of_subpatterns_do_match(arm.pat, &body, &infer) + && types_of_subpatterns_do_match(arm.pat, &body, &self.infer) { // If we had a NotUsefulMatchArm diagnostic, we could // check the usefulness of each pattern as we added it From 5e8c586f3b8c1fc43e293a53317bafe9df0b77fe Mon Sep 17 00:00:00 2001 From: iDawer Date: Fri, 10 Mar 2023 18:21:54 +0500 Subject: [PATCH 3/4] Refactor hir::diagnostics::MissingMatchArms fields, better naming --- crates/hir/src/diagnostics.rs | 3 +-- crates/hir/src/lib.rs | 8 +++++--- crates/ide-diagnostics/src/handlers/missing_match_arms.rs | 4 +--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/crates/hir/src/diagnostics.rs b/crates/hir/src/diagnostics.rs index c257ee2ae3a..8f019a81b2d 100644 --- a/crates/hir/src/diagnostics.rs +++ b/crates/hir/src/diagnostics.rs @@ -199,8 +199,7 @@ pub struct MismatchedArgCount { #[derive(Debug)] pub struct MissingMatchArms { - pub file: HirFileId, - pub match_expr: AstPtr, + pub scrutinee_expr: InFile>, pub uncovered_patterns: String, } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 92b31031ca1..72a4829445d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1628,11 +1628,13 @@ impl DefWithBody { if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) { - if let Some(match_expr) = match_expr.expr() { + if let Some(scrut_expr) = match_expr.expr() { acc.push( MissingMatchArms { - file: source_ptr.file_id, - match_expr: AstPtr::new(&match_expr), + scrutinee_expr: InFile::new( + source_ptr.file_id, + AstPtr::new(&scrut_expr), + ), uncovered_patterns, } .into(), diff --git a/crates/ide-diagnostics/src/handlers/missing_match_arms.rs b/crates/ide-diagnostics/src/handlers/missing_match_arms.rs index 5ded2f22309..ac4463331f2 100644 --- a/crates/ide-diagnostics/src/handlers/missing_match_arms.rs +++ b/crates/ide-diagnostics/src/handlers/missing_match_arms.rs @@ -1,5 +1,3 @@ -use hir::InFile; - use crate::{Diagnostic, DiagnosticsContext}; // Diagnostic: missing-match-arm @@ -12,7 +10,7 @@ pub(crate) fn missing_match_arms( Diagnostic::new( "missing-match-arm", format!("missing match arm: {}", d.uncovered_patterns), - ctx.sema.diagnostics_display_range(InFile::new(d.file, d.match_expr.clone().into())).range, + ctx.sema.diagnostics_display_range(d.scrutinee_expr.clone().map(Into::into)).range, ) } From 17b9d35b3123592320fa64ac81e7483a69a6b4d7 Mon Sep 17 00:00:00 2001 From: iDawer Date: Fri, 10 Mar 2023 18:43:50 +0500 Subject: [PATCH 4/4] Refactor: Distinguish scrutinee expression from match expression --- crates/hir-ty/src/diagnostics/expr.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs index c728719997c..2e9066788cf 100644 --- a/crates/hir-ty/src/diagnostics/expr.rs +++ b/crates/hir-ty/src/diagnostics/expr.rs @@ -147,15 +147,15 @@ impl ExprValidator { fn validate_match( &mut self, - id: ExprId, match_expr: ExprId, + scrutinee_expr: ExprId, arms: &[MatchArm], db: &dyn HirDatabase, ) { let body = db.body(self.owner); - let match_expr_ty = &self.infer[match_expr]; - if match_expr_ty.is_unknown() { + let scrut_ty = &self.infer[scrutinee_expr]; + if scrut_ty.is_unknown() { return; } @@ -167,17 +167,17 @@ impl ExprValidator { for arm in arms { if let Some(pat_ty) = self.infer.type_of_pat.get(arm.pat) { // We only include patterns whose type matches the type - // of the match expression. If we had an InvalidMatchArmPattern + // of the scrutinee expression. If we had an InvalidMatchArmPattern // diagnostic or similar we could raise that in an else // block here. // // When comparing the types, we also have to consider that rustc - // will automatically de-reference the match expression type if + // will automatically de-reference the scrutinee expression type if // necessary. // // FIXME we should use the type checker for this. - if (pat_ty == match_expr_ty - || match_expr_ty + if (pat_ty == scrut_ty + || scrut_ty .as_reference() .map(|(match_expr_ty, ..)| match_expr_ty == pat_ty) .unwrap_or(false)) @@ -205,7 +205,7 @@ impl ExprValidator { return; } - let report = compute_match_usefulness(&cx, &m_arms, match_expr_ty); + let report = compute_match_usefulness(&cx, &m_arms, scrut_ty); // FIXME Report unreacheble arms // https://github.com/rust-lang/rust/blob/f31622a50/compiler/rustc_mir_build/src/thir/pattern/check_match.rs#L200 @@ -213,8 +213,8 @@ impl ExprValidator { let witnesses = report.non_exhaustiveness_witnesses; if !witnesses.is_empty() { self.diagnostics.push(BodyValidationDiagnostic::MissingMatchArms { - match_expr: id, - uncovered_patterns: missing_match_arms(&cx, match_expr_ty, witnesses, arms), + match_expr, + uncovered_patterns: missing_match_arms(&cx, scrut_ty, witnesses, arms), }); } }