Skip to content

Fix closures.rs indent double-counting for wrapped params with return… - #7015

Open
saberoueslati wants to merge 1 commit into
rust-lang:mainfrom
saberoueslati:issue-7012-nested-closure-return-type
Open

Fix closures.rs indent double-counting for wrapped params with return…#7015
saberoueslati wants to merge 1 commit into
rust-lang:mainfrom
saberoueslati:issue-7012-nested-closure-return-type

Conversation

@saberoueslati

Copy link
Copy Markdown

Summary

Fixes #7012: rustfmt gives up formatting a statement when a method-chain argument is a closure whose parameter list wraps onto multiple lines and has an explicit return type, at moderate nesting depth. The reported symptom is that nothing changes and a misindented .try_collect()? is left as-is.

Root cause

In rewrite_closure_fn_decl (src/closures.rs), when the closure's parameters wrap, the return type is placed on its own line prefixed with param_offset indentation. last_line_width(&prefix) then returns an absolute column rather than a width. The caller applies this value via shape.offset_left(extra_offset, span), which adds it on top of shape.indent, double counting the indent. For the reported code this makes extra_offset exceed shape.width, the rewrite returns ExceedsMaxWidth, and the error propagates up through the closure, the .map() call, the chain, and the let statement, so rustfmt falls back to emitting the original source untouched.

Fix

When the prefix wraps, subtract shape.used_width() from the computed offset so it is relative to the shape instead of absolute. This is gated behind style_edition >= Edition2027 because it changes the formatting of closures that already succeed today (a probe closure with a multiline param list and -> T return type joins its body onto one line up to 86 chars instead of 78). This follows the same gating precedent as #6835 for issue #6831.

Testing

  • Added tests/source/issue_7012_style_edition_2024.rs and matching target, showing the current stable behavior is unchanged (rustfmt still leaves the statement unformatted).
  • Added tests/source/issue_7012_style_edition_2027.rs and matching target, showing .try_collect()? correctly aligned under style_edition=2027.
  • Verified the new target file is idempotent under --check.
  • Ran the full cargo test suite locally, all system, idempotence, and integration tests pass with no changes to any pre-existing target file.
  • Confirmed the new test files are actually exercised by temporarily corrupting the 2027 target and observing a test failure, then restoring it.

Changelog

Added an entry under Unreleased > Fixed noting the style edition 2027 gated fix.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 6, 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.

rustfmt gives up on code with moderate nesting, destructuring closure arg, and nested return type

2 participants