Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions crates/ide-assists/src/handlers/move_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext<'_,
.rfold(None, |else_branch, arm| {
if let Some(guard) = arm.guard() {
let then_branch = crate::utils::wrap_block(&arm.expr()?, &make);

let empty_if = make.expr_if(make.expr_unit(), make.expr_empty_block(), None);
let empty_condition = empty_if.condition()?;

let guard_condition = guard.condition()?.reset_indent();
let guard_condition = if guard_condition
.needs_parens_in_place_of(empty_if.syntax(), empty_condition.syntax())

@ChayimFriedman2 ChayimFriedman2 Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. I forgot that we don't have the if available.

So I'm torn between two options: creating the if first and using the SyntaxEditor to wrap in parens if needed, or the previous way. The current way is not good IMO.

View changes since the review

{
make.expr_paren(guard_condition).into()
} else {
guard_condition

@A4-Tacks A4-Tacks Sep 11, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use utils::wrap_paren_in_guard_chain()
And this should be renamed as utils::wrap_paren_in_iflet_chain()

View changes since the review

};
Some(make.expr_if(guard_condition, then_branch, else_branch).into())
} else {
arm.expr().map(|it| crate::utils::wrap_block(&it, &make).into())
Expand Down Expand Up @@ -355,6 +366,60 @@ fn main() {
);
}

#[test]
fn move_guard_to_arm_body_parenthesizes_struct_literal() {
check_assist(
move_guard_to_arm_body,
r#"
struct S { f: i32 }
fn main() {
match 92 {
x $0if S { f: x } == y => false,
_ => true
}
}
"#,
r#"
struct S { f: i32 }
fn main() {
match 92 {
x => if (S { f: x } == y) {
false
},
_ => true
}
}
"#,
);
}

#[test]
fn move_guard_to_arm_body_parenthesizes_trailing_struct_literal() {
check_assist(
move_guard_to_arm_body,
r#"
struct S { f: i32 }
fn main() {
match 92 {
x $0if y == S { f: x } => false,
_ => true
}
}
"#,
r#"
struct S { f: i32 }
fn main() {
match 92 {
x => if (y == S { f: x }) {
false
},
_ => true
}
}
"#,
);
}

#[test]
fn move_guard_to_arm_body_works() {
check_assist(
Expand Down
Loading