Skip to content

Commit 60153f6

Browse files
committed
Fix closure in match not applicable for add_braces
Example --- **Not applicable**: ```rust fn foo() { match () { () => { t(|n|$0 n + 100); } } } ```
1 parent 1f4e5e8 commit 60153f6

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

crates/ide-assists/src/handlers/add_braces.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use either::Either;
12
use syntax::{
23
AstNode,
34
ast::{self, edit_in_place::Indent, syntax_factory::SyntaxFactory},
@@ -59,15 +60,16 @@ enum ParentType {
5960
}
6061

6162
fn get_replacement_node(ctx: &AssistContext<'_>) -> Option<(ParentType, ast::Expr)> {
62-
if let Some(match_arm) = ctx.find_node_at_offset::<ast::MatchArm>() {
63+
let node = ctx.find_node_at_offset::<Either<ast::MatchArm, ast::ClosureExpr>>()?;
64+
if let Either::Left(match_arm) = &node {
6365
let match_arm_expr = match_arm.expr()?;
6466

6567
if matches!(match_arm_expr, ast::Expr::BlockExpr(_)) {
6668
return None;
6769
}
6870

6971
return Some((ParentType::MatchArmExpr, match_arm_expr));
70-
} else if let Some(closure_expr) = ctx.find_node_at_offset::<ast::ClosureExpr>() {
72+
} else if let Either::Right(closure_expr) = &node {
7173
let body = closure_expr.body()?;
7274

7375
if matches!(body, ast::Expr::BlockExpr(_)) {
@@ -105,6 +107,33 @@ fn foo() {
105107
);
106108
}
107109

110+
#[test]
111+
fn suggest_add_braces_for_closure_in_match() {
112+
check_assist(
113+
add_braces,
114+
r#"
115+
fn foo() {
116+
match () {
117+
() => {
118+
t(|n|$0 n + 100);
119+
}
120+
}
121+
}
122+
"#,
123+
r#"
124+
fn foo() {
125+
match () {
126+
() => {
127+
t(|n| {
128+
n + 100
129+
});
130+
}
131+
}
132+
}
133+
"#,
134+
);
135+
}
136+
108137
#[test]
109138
fn no_assist_for_closures_with_braces() {
110139
check_assist_not_applicable(

0 commit comments

Comments
 (0)