From cab91480b22156bd7dce3f79c93f2af373fcdfcf Mon Sep 17 00:00:00 2001 From: Young-Flash <871946895@qq.com> Date: Sun, 26 Nov 2023 22:57:30 +0800 Subject: [PATCH 1/2] fix: don't make `MissingMatchArms` diagnostic for empty match body --- crates/hir/src/lib.rs | 25 +++++++++++-------- .../src/handlers/missing_match_arms.rs | 12 +++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 1bfbf7212bf0..920c88ccfbc7 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1914,17 +1914,20 @@ impl DefWithBody { if let ast::Expr::MatchExpr(match_expr) = &source_ptr.value.to_node(&root) { - if let Some(scrut_expr) = match_expr.expr() { - acc.push( - MissingMatchArms { - scrutinee_expr: InFile::new( - source_ptr.file_id, - AstPtr::new(&scrut_expr), - ), - uncovered_patterns, - } - .into(), - ); + match match_expr.expr() { + Some(scrut_expr) if match_expr.match_arm_list().is_some() => { + acc.push( + MissingMatchArms { + 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 84267d3d9069..3f2a6eafb0ba 100644 --- a/crates/ide-diagnostics/src/handlers/missing_match_arms.rs +++ b/crates/ide-diagnostics/src/handlers/missing_match_arms.rs @@ -25,6 +25,18 @@ mod tests { crate::tests::check_diagnostics(ra_fixture) } + #[test] + fn empty_body() { + check_diagnostics_no_bails( + r#" +fn main() { + match 0; + //^ error: Syntax Error: expected `{` +} +"#, + ); + } + #[test] fn empty_tuple() { check_diagnostics_no_bails( From b46f37854efd0111a5a150f33225c7147d02af0a Mon Sep 17 00:00:00 2001 From: Young-Flash <871946895@qq.com> Date: Thu, 30 Nov 2023 19:35:25 +0800 Subject: [PATCH 2/2] update: filter out syntax error in test --- .../src/handlers/missing_match_arms.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/ide-diagnostics/src/handlers/missing_match_arms.rs b/crates/ide-diagnostics/src/handlers/missing_match_arms.rs index 3f2a6eafb0ba..ef6a273ed8e6 100644 --- a/crates/ide-diagnostics/src/handlers/missing_match_arms.rs +++ b/crates/ide-diagnostics/src/handlers/missing_match_arms.rs @@ -17,7 +17,10 @@ pub(crate) fn missing_match_arms( #[cfg(test)] mod tests { - use crate::tests::check_diagnostics; + use crate::{ + tests::{check_diagnostics, check_diagnostics_with_config}, + DiagnosticsConfig, + }; #[track_caller] fn check_diagnostics_no_bails(ra_fixture: &str) { @@ -27,11 +30,13 @@ mod tests { #[test] fn empty_body() { - check_diagnostics_no_bails( + let mut config = DiagnosticsConfig::test_sample(); + config.disabled.insert("syntax-error".to_string()); + check_diagnostics_with_config( + config, r#" fn main() { match 0; - //^ error: Syntax Error: expected `{` } "#, );