Skip to content

Fix hybrid formatting of struct patterns containing ..#6875

Open
Souradip121 wants to merge 1 commit intorust-lang:mainfrom
Souradip121:fix-struct-pat-ellipsis-formatting
Open

Fix hybrid formatting of struct patterns containing ..#6875
Souradip121 wants to merge 1 commit intorust-lang:mainfrom
Souradip121:fix-struct-pat-ellipsis-formatting

Conversation

@Souradip121
Copy link
Copy Markdown

Summary

  • struct_lit_shape computes h_shape.width already reduced by len(", ..") to reserve space for the ellipsis suffix. After write_list, we then append ", .." to fields_str, making it 4 chars longer. wrap_struct_field was comparing this augmented string against the original (already-reduced) one_line_width, so patterns with fields near the width budget got incorrectly flagged as too wide and wrapped vertically.
  • Fix: introduce adjusted_one_line_width = one_line_width + ellipsis_str.len() before the wrap_struct_field call, so the threshold matches what's actually in fields_str.
  • Updated three single_line_let_else_max_width test targets and match.rs where the fix causes struct patterns with .. to stay on one line.
  • Reformatted src/imports.rs and src/visitor.rs to match the new output (required by the self-test).

Test Plan

  • cargo test passes
  • tests/source/issue-6827.rs covers all three cases from the issue: single-line, previously-hybrid (now single-line), and genuinely-too-wide (vertical)
  • Idempotency check passes for all new and updated targets

Fixes #6827

When a struct pattern's fields were within struct_lit_width but within
4 chars of it, appending ", .." would push fields_str.len() over
one_line_width in wrap_struct_field, producing a hybrid format:

  let X {
      field1, field2x, ..
  } = x;

The root cause: struct_lit_shape already subtracts ellipsis_str.len()
from h_shape.width (via suffix_width), but after appending ", .." to
fields_str the wrap_struct_field check compared the augmented string
against the already-reduced one_line_width threshold.

Fix by adjusting one_line_width to account for the appended ellipsis
before passing it to wrap_struct_field. Update affected test targets
and rustfmt source files formatted by the new behaviour.

Fixes rust-lang#6827
@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Apr 21, 2026
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.

Struct patterns containing .. are sometimes formatted neither vertically nor on a single line

2 participants