Merge pull request #21485 from A4-Tacks/move-guard-clean-block

Improve move_guard redundanted block
This commit is contained in:
Shoyu Vanilla (Flint) 2026-01-20 06:14:13 +00:00 committed by GitHub
commit 92903fa647
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 4 deletions

View file

@ -49,7 +49,7 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext<'_>)
let guard_condition = guard.condition()?.reset_indent();
let arm_expr = match_arm.expr()?;
let then_branch = make::block_expr(None, Some(arm_expr.reset_indent().indent(1.into())));
let then_branch = crate::utils::wrap_block(&arm_expr);
let if_expr = make::expr_if(guard_condition, then_branch, None).indent(arm_expr.indent_level());
let target = guard.syntax().text_range();
@ -344,6 +344,35 @@ fn main() {
);
}
#[test]
fn move_guard_to_block_arm_body_works() {
check_assist(
move_guard_to_arm_body,
r#"
fn main() {
match 92 {
x $0if x > 10 => {
let _ = true;
false
},
_ => true
}
}
"#,
r#"
fn main() {
match 92 {
x => if x > 10 {
let _ = true;
false
},
_ => true
}
}
"#,
);
}
#[test]
fn move_let_guard_to_arm_body_works() {
check_assist(
@ -395,9 +424,7 @@ fn main() {
&& true
&& true {
{
{
false
}
false
}
},
_ => true

View file

@ -86,6 +86,17 @@ pub fn extract_trivial_expression(block_expr: &ast::BlockExpr) -> Option<ast::Ex
None
}
pub(crate) fn wrap_block(expr: &ast::Expr) -> ast::BlockExpr {
if let ast::Expr::BlockExpr(block) = expr
&& let Some(first) = block.syntax().first_token()
&& first.kind() == T!['{']
{
block.reset_indent()
} else {
make::block_expr(None, Some(expr.reset_indent().indent(1.into())))
}
}
/// This is a method with a heuristics to support test methods annotated with custom test annotations, such as
/// `#[test_case(...)]`, `#[tokio::test]` and similar.
/// Also a regular `#[test]` annotation is supported.