Unnamed repository; edit this file 'description' to name the repository.
Add parent_match method to node_ext
A4-Tacks 4 months ago
parent e1747f9 · commit f0055f6
-rw-r--r--crates/ide-completion/src/context/analysis.rs6
-rw-r--r--crates/syntax/src/ast/node_ext.rs10
2 files changed, 13 insertions, 3 deletions
diff --git a/crates/ide-completion/src/context/analysis.rs b/crates/ide-completion/src/context/analysis.rs
index e48c433fa4..f92ce5373b 100644
--- a/crates/ide-completion/src/context/analysis.rs
+++ b/crates/ide-completion/src/context/analysis.rs
@@ -737,17 +737,17 @@ fn expected_type_and_name<'db>(
ast::MatchArm(it) => {
let on_arrow = previous_non_trivia_token(token.clone()).is_some_and(|it| T![=>] == it.kind());
let in_body = it.expr().is_some_and(|it| it.syntax().text_range().contains_range(token.text_range()));
- let match_expr = it.syntax().ancestors().nth(2).and_then(ast::MatchExpr::cast);
+ let match_expr = it.parent_match();
let ty = if on_arrow || in_body {
// match foo { ..., pat => $0 }
cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
- match_expr.and_then(|it| sema.type_of_expr(&it.into()))
+ sema.type_of_expr(&match_expr.into())
} else {
// match foo { $0 }
cov_mark::hit!(expected_type_match_arm_without_leading_char);
- match_expr.and_then(|it| it.expr()).and_then(|e| sema.type_of_expr(&e))
+ match_expr.expr().and_then(|e| sema.type_of_expr(&e))
}.map(TypeInfo::original);
(ty, None)
},
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index b872221bf7..800dd5f4ac 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -1096,6 +1096,16 @@ impl ast::MatchGuard {
}
}
+impl ast::MatchArm {
+ pub fn parent_match(&self) -> ast::MatchExpr {
+ self.syntax()
+ .parent()
+ .and_then(|it| it.parent())
+ .and_then(ast::MatchExpr::cast)
+ .expect("MatchArms are always nested in MatchExprs")
+ }
+}
+
impl From<ast::Item> for ast::AnyHasAttrs {
fn from(node: ast::Item) -> Self {
Self::new(node)