Skip to content

Commit 0bb074a

Browse files
bors[bot]Brandon
andauthored
Merge #8620
8620: Remove unnecessary braces for extracted block expression r=Veykril a=brandondong This change addresses the first bullet point of #7839. Specifically, when extracting block expressions, remove the unneeded extra braces inside the generated function. Co-authored-by: Brandon <brandondong604@hotmail.com>
2 parents d1c9bd1 + 1713f4c commit 0bb074a

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,19 @@ fn make_body(
12271227
FunctionBody::Expr(expr) => {
12281228
let expr = rewrite_body_segment(ctx, &fun.params, &handler, expr.syntax());
12291229
let expr = ast::Expr::cast(expr).unwrap();
1230-
let expr = expr.dedent(old_indent).indent(IndentLevel(1));
1230+
match expr {
1231+
ast::Expr::BlockExpr(block) => {
1232+
// If the extracted expression is itself a block, there is no need to wrap it inside another block.
1233+
let block = block.dedent(old_indent);
1234+
// Recreate the block for formatting consistency with other extracted functions.
1235+
make::block_expr(block.statements(), block.tail_expr())
1236+
}
1237+
_ => {
1238+
let expr = expr.dedent(old_indent).indent(IndentLevel(1));
12311239

1232-
make::block_expr(Vec::new(), Some(expr))
1240+
make::block_expr(Vec::new(), Some(expr))
1241+
}
1242+
}
12331243
}
12341244
FunctionBody::Span { parent, text_range } => {
12351245
let mut elements: Vec<_> = parent
@@ -1544,7 +1554,7 @@ fn foo() {
15441554
}
15451555
15461556
fn $0fun_name() -> i32 {
1547-
{ 1 + 1 }
1557+
1 + 1
15481558
}"#,
15491559
);
15501560
}
@@ -2526,17 +2536,15 @@ fn foo() {
25262536
}
25272537
25282538
fn $0fun_name(n: &mut i32) {
2529-
{
2530-
*n += *n;
2531-
bar(*n);
2532-
bar(*n+1);
2533-
bar(*n**n);
2534-
bar(&*n);
2535-
n.inc();
2536-
let v = n;
2537-
*v = v.succ();
2538-
n.succ();
2539-
}
2539+
*n += *n;
2540+
bar(*n);
2541+
bar(*n+1);
2542+
bar(*n**n);
2543+
bar(&*n);
2544+
n.inc();
2545+
let v = n;
2546+
*v = v.succ();
2547+
n.succ();
25402548
}",
25412549
);
25422550
}

0 commit comments

Comments
 (0)