Skip to content

fix(autofmt): don't duplicate pretty continuation lines after multi-line source match#5546

Open
Not-Sarthak wants to merge 1 commit into
DioxusLabs:mainfrom
Not-Sarthak:fix/5523-autofmt-cfg-let
Open

fix(autofmt): don't duplicate pretty continuation lines after multi-line source match#5546
Not-Sarthak wants to merge 1 commit into
DioxusLabs:mainfrom
Not-Sarthak:fix/5523-autofmt-cfg-let

Conversation

@Not-Sarthak
Copy link
Copy Markdown

Summary

Fixes #5523.

dx fmt corrupted RSX event handler blocks containing #[cfg]-gated let bindings around a move |_| closure. When prettier_please broke a long statement at a different line position than the source did, the multi-line accumulator in write_partial_expr consumed every source line for the logical statement but the outer iterator kept walking pretty lines, re-emitted the trailing continuation (.clone();), and advanced the source pointer past unrelated statements — producing invalid Rust:

let X = Y.clone();
    .clone();
if let Err(err) = ... {
    err
    ));

Fix

pretty.lines() → peekable. After emitting a multi-line verbatim source block whose accumulated content extends beyond the matched pretty line, consume the pretty continuation lines whose content is already covered.

Test plan

  • New fixture tests/samples/cfg_let_event_block.rsx wired into twoway!
  • New cfg_let_event_block_is_idempotent test
  • 84 existing autofmt tests pass
  • cargo clippy --workspace --all-features --all-targets -- -D warnings clean
  • cargo fmt --all -- --check clean
  • cargo test --workspace --exclude dioxus-desktop clean (334 test bins, 0 failures)
  • Stress-tested 8 adversarial inputs (cfg-lets in various positions, async closures, long method chains, format!() macros) — all produce valid Rust

…ine source match

When prettier_please breaks a statement at a different line position than
the source did, the multi-line accumulator in write_partial_expr consumed
every source line for the logical statement but the outer iterator kept
walking pretty lines, re-emitting trailing continuations (e.g. `.clone();`)
and advancing the source pointer past unrelated statements - producing
invalid Rust.

Convert pretty.lines() to a peekable iterator and, after emitting a
multi-line verbatim source block whose accumulated content extends beyond
the matched pretty line, consume the following pretty continuation lines
whose content is already covered by the accumulated source.

Fixes DioxusLabs#5523.
@Not-Sarthak Not-Sarthak requested a review from a team as a code owner May 10, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dx fmt corrupts RSX onclick closures with cfg-gated let bindings

1 participant