Skip to content

Commit f96156e

Browse files
authored
Rollup merge of rust-lang#114764 - pitaj:style-delimited-expressions, r=joshtriplett
[style edition 2024] Combine all delimited exprs as last argument Closes rust-lang/style-team#149 If this is merged, the rustfmt option `overflow_delimited_expr` should be enabled by default in style edition 2024. [Rendered](https://github.com/pitaj/rust/blob/style-delimited-expressions/src/doc/style-guide/src/expressions.md#combinable-expressions) r? joshtriplett
2 parents f6ee4bf + bed0c9d commit f96156e

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

Diff for: src/doc/style-guide/src/editions.md

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ For a full history of changes in the Rust 2024 style edition, see the git
3636
history of the style guide. Notable changes in the Rust 2024 style edition
3737
include:
3838

39+
- [#114764](https://github.com/rust-lang/rust/pull/114764) As the last member
40+
of a delimited expression, delimited expressions are generally combinable,
41+
regardless of the number of members. Previously only applied with exactly
42+
one member (except for closures with explicit blocks).
3943
- Miscellaneous `rustfmt` bugfixes.
4044
- Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order).
4145
- Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase".

Diff for: src/doc/style-guide/src/expressions.md

+48-7
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,11 @@ E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
818818

819819
## Combinable expressions
820820

821-
Where a function call has a single argument, and that argument is formatted
822-
across multiple-lines, format the outer call as if it were a single-line call,
821+
When the last argument in a function call is formatted across
822+
multiple-lines, format the outer call as if it were a single-line call,
823823
if the result fits. Apply the same combining behaviour to any similar
824824
expressions which have multi-line, block-indented lists of sub-expressions
825-
delimited by parentheses (e.g., macros or tuple struct literals). E.g.,
825+
delimited by parentheses, brackets, or braces. E.g.,
826826

827827
```rust
828828
foo(bar(
@@ -848,20 +848,61 @@ let arr = [combinable(
848848
an_expr,
849849
another_expr,
850850
)];
851+
852+
let x = Thing(an_expr, another_expr, match cond {
853+
A => 1,
854+
B => 2,
855+
});
856+
857+
let x = format!("Stuff: {}", [
858+
an_expr,
859+
another_expr,
860+
]);
861+
862+
let x = func(an_expr, another_expr, SomeStruct {
863+
field: this_is_long,
864+
another_field: 123,
865+
});
851866
```
852867

853868
Apply this behavior recursively.
854869

855-
For a function with multiple arguments, if the last argument is a multi-line
856-
closure with an explicit block, there are no other closure arguments, and all
857-
the arguments and the first line of the closure fit on the first line, use the
858-
same combining behavior:
870+
If the last argument is a multi-line closure with an explicit block,
871+
only apply the combining behavior if there are no other closure arguments.
859872

860873
```rust
874+
// Combinable
861875
foo(first_arg, x, |param| {
862876
action();
863877
foo(param)
864878
})
879+
// Not combinable, because the closure is not the last argument
880+
foo(
881+
first_arg,
882+
|param| {
883+
action();
884+
foo(param)
885+
},
886+
whatever,
887+
)
888+
// Not combinable, because the first line of the closure does not fit
889+
foo(
890+
first_arg,
891+
x,
892+
move |very_long_param_causing_line_to_overflow| -> Bar {
893+
action();
894+
foo(param)
895+
},
896+
)
897+
// Not combinable, because there is more than one closure argument
898+
foo(
899+
first_arg,
900+
|x| x.bar(),
901+
|param| {
902+
action();
903+
foo(param)
904+
},
905+
)
865906
```
866907

867908
## Ranges

0 commit comments

Comments
 (0)