From 5a4432ab8976a8facd76d39fd7f358e392bc4a14 Mon Sep 17 00:00:00 2001 From: saberoueslati Date: Thu, 6 Aug 2026 23:30:40 +0100 Subject: [PATCH] Fix closures.rs indent double-counting for wrapped params with return type --- CHANGELOG.md | 3 +++ src/closures.rs | 11 +++++++- tests/source/issue_7012_style_edition_2024.rs | 27 +++++++++++++++++++ tests/source/issue_7012_style_edition_2027.rs | 27 +++++++++++++++++++ tests/target/issue_7012_style_edition_2024.rs | 27 +++++++++++++++++++ tests/target/issue_7012_style_edition_2027.rs | 27 +++++++++++++++++++ 6 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue_7012_style_edition_2024.rs create mode 100644 tests/source/issue_7012_style_edition_2027.rs create mode 100644 tests/target/issue_7012_style_edition_2024.rs create mode 100644 tests/target/issue_7012_style_edition_2027.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b6d9a3b8dc..b28684c43a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Fixed +- (Style Edition 2027) Properly format closures with wrapped parameters and an explicit return type instead of leaving the enclosing expression unformatted. Issue: [#7012](https://github.com/rust-lang/rustfmt/issues/7012). + ## [1.10.0] 2026-07-21 diff --git a/src/closures.rs b/src/closures.rs index 9bd319a3a5f..0f3d7ea1198 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -351,7 +351,16 @@ fn rewrite_closure_fn_decl( prefix.push_str(&ret_str); } // 1 = space between `|...|` and body. - let extra_offset = last_line_width(&prefix) + 1; + let last_line = last_line_width(&prefix) + 1; + let extra_offset = + if prefix.contains('\n') && context.config.style_edition() >= StyleEdition::Edition2027 { + // The return type was pushed onto a line of its own, so `last_line_width` measures + // from column zero. Callers apply this offset on top of `shape`, so subtract the + // width already accounted for by `shape` to avoid counting the indent twice. + last_line.saturating_sub(shape.used_width()) + } else { + last_line + }; Ok((prefix, extra_offset)) } diff --git a/tests/source/issue_7012_style_edition_2024.rs b/tests/source/issue_7012_style_edition_2024.rs new file mode 100644 index 00000000000..daab087e126 --- /dev/null +++ b/tests/source/issue_7012_style_edition_2024.rs @@ -0,0 +1,27 @@ +// rustfmt-style_edition: 2024 + +fn test() { + if outer { + if inner { + if even_more_inner { + let items = page + .into_iter() + .map( + |list_media_response::ListEntry { + cdn, + media_id, + length, + }| + -> Result<_, RequestError> { + Ok(ListMediaItem { + cdn, + media_id, + object_length: length, + }) + }, + ) + .try_collect()?; + } + } + } +} diff --git a/tests/source/issue_7012_style_edition_2027.rs b/tests/source/issue_7012_style_edition_2027.rs new file mode 100644 index 00000000000..e71f47d0463 --- /dev/null +++ b/tests/source/issue_7012_style_edition_2027.rs @@ -0,0 +1,27 @@ +// rustfmt-style_edition: 2027 + +fn test() { + if outer { + if inner { + if even_more_inner { + let items = page + .into_iter() + .map( + |list_media_response::ListEntry { + cdn, + media_id, + length, + }| + -> Result<_, RequestError> { + Ok(ListMediaItem { + cdn, + media_id, + object_length: length, + }) + }, + ) + .try_collect()?; + } + } + } +} diff --git a/tests/target/issue_7012_style_edition_2024.rs b/tests/target/issue_7012_style_edition_2024.rs new file mode 100644 index 00000000000..daab087e126 --- /dev/null +++ b/tests/target/issue_7012_style_edition_2024.rs @@ -0,0 +1,27 @@ +// rustfmt-style_edition: 2024 + +fn test() { + if outer { + if inner { + if even_more_inner { + let items = page + .into_iter() + .map( + |list_media_response::ListEntry { + cdn, + media_id, + length, + }| + -> Result<_, RequestError> { + Ok(ListMediaItem { + cdn, + media_id, + object_length: length, + }) + }, + ) + .try_collect()?; + } + } + } +} diff --git a/tests/target/issue_7012_style_edition_2027.rs b/tests/target/issue_7012_style_edition_2027.rs new file mode 100644 index 00000000000..3a4bfa1d526 --- /dev/null +++ b/tests/target/issue_7012_style_edition_2027.rs @@ -0,0 +1,27 @@ +// rustfmt-style_edition: 2027 + +fn test() { + if outer { + if inner { + if even_more_inner { + let items = page + .into_iter() + .map( + |list_media_response::ListEntry { + cdn, + media_id, + length, + }| + -> Result<_, RequestError> { + Ok(ListMediaItem { + cdn, + media_id, + object_length: length, + }) + }, + ) + .try_collect()?; + } + } + } +}