Skip to content

Commit 60a783b

Browse files
committed
[style 2024] Combine all delimited exprs as last argument
1 parent 1e836d1 commit 60a783b

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/doc/style-guide/src/editions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ 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+
- As the last argument of a function call, delimited expressions are generally
40+
combinable, regardless of the number of function arguments. Previously only
41+
applied with exactly one argument (except for closures with explicit blocks).
3942
- Miscellaneous `rustfmt` bugfixes.
4043

4144
## Rust 2015/2018/2021 style edition

src/doc/style-guide/src/expressions.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,11 @@ E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
801801

802802
## Combinable expressions
803803

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

810810
```rust
811811
foo(bar(
@@ -831,20 +831,56 @@ let arr = [combinable(
831831
an_expr,
832832
another_expr,
833833
)];
834+
835+
let x = Thing(an_expr, another_expr, match cond {
836+
A => 1,
837+
B => 2,
838+
});
839+
840+
let x = format!("Stuff: {}", [
841+
an_expr,
842+
another_expr,
843+
]);
834844
```
835845

836846
Apply this behavior recursively.
837847

838-
For a function with multiple arguments, if the last argument is a multi-line
839-
closure with an explicit block, there are no other closure arguments, and all
840-
the arguments and the first line of the closure fit on the first line, use the
841-
same combining behavior:
848+
If the last argument is a multi-line closure with an explicit block,
849+
only apply the combining behavior if there are no other closure arguments.
842850

843851
```rust
852+
// Combinable
844853
foo(first_arg, x, |param| {
845854
action();
846855
foo(param)
847856
})
857+
// Not combinable, because the closure is not the last argument
858+
foo(
859+
first_arg,
860+
|param| {
861+
action();
862+
foo(param)
863+
},
864+
whatever,
865+
)
866+
// Not combinable, because the first line of the closure does not fit
867+
foo(
868+
first_arg,
869+
x,
870+
move |very_long_param_causing_line_to_overflow| -> Bar {
871+
action();
872+
foo(param)
873+
},
874+
)
875+
// Not combinable, because there is more than one closure argument
876+
foo(
877+
first_arg,
878+
|x| x.bar(),
879+
|param| {
880+
action();
881+
foo(param)
882+
},
883+
)
848884
```
849885

850886
## Ranges

0 commit comments

Comments
 (0)