Skip to content

fix: Panic on move_guard_to_arm_body with struct literals - #23306

Open
Wilfred wants to merge 1 commit into
rust-lang:masterfrom
Wilfred:fix/move-guard-struct-literal-parens
Open

fix: Panic on move_guard_to_arm_body with struct literals#23306
Wilfred wants to merge 1 commit into
rust-lang:masterfrom
Wilfred:fix/move-guard-struct-literal-parens

Conversation

@Wilfred

@Wilfred Wilfred commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Rust syntax requires parentheses on struct literals ("exterior struct literals") in if statements.

Previously, we'd try to compute the edit for the move_guard_to_arm_body assist, try to parse if foo == SomeStruct { x: 123 } and panic.

Instead, wrap the expression in parentheses so it's legal Rust. This fixes the panic and ensures the assist produces well-formed code.

This assist doesn't exactly match the suggestions in rustc, but it's still valid output and much simpler to implement.

(rustc suggests if foo == (SomeStruct { x: 123 }) whereas we now produce
if (foo == SomeStruct { x: 123 }).)

AI disclosure: Code written with assistance by Opus 5, but comments, commit message and review by me.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 7, 2026
@Wilfred
Wilfred force-pushed the fix/move-guard-struct-literal-parens branch from c4a8fd2 to a867960 Compare September 7, 2026 13:43
let guard_condition = guard.condition()?.reset_indent();

let mut guard_condition = guard.condition()?.reset_indent();
if guard_condition.contains_exterior_struct_lit() {

@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.

You should use needs_parens_in() instead.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Using needs_parens_in_place_of() made the changes a bit simpler, so I went with that instead. Let me know what you think :)

Rust syntax requires parentheses on struct literals in if statements.

Previously, we'd try to compute the edit for the
move_guard_to_arm_body assist, try to parse `if foo == SomeStruct { x:
123 }` and panic.

Instead, wrap the expression in parentheses so it's legal Rust. This
fixes the panic and ensures the assist produces well-formed code.

This assist doesn't exactly match the suggestions in rustc, but it's
still valid output and much simpler to implement.

(rustc suggests `if foo == (SomeStruct { x: 123 })` whereas we now produce
`if (foo == SomeStruct { x: 123 })`.)

AI disclosure: Code written with assistance by Opus 5, but comments,
commit message and review by me.
@Wilfred
Wilfred force-pushed the fix/move-guard-struct-literal-parens branch from a867960 to a6040d3 Compare September 7, 2026 15:01

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants