Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bed0c9d

Browse files
committedNov 30, 2023
[style 2024] Combine all last arg delimited exprs
1 parent 1e836d1 commit bed0c9d

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed
 

‎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

4145
## Rust 2015/2018/2021 style edition

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

+48-7
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,61 @@ 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+
]);
844+
845+
let x = func(an_expr, another_expr, SomeStruct {
846+
field: this_is_long,
847+
another_field: 123,
848+
});
834849
```
835850

836851
Apply this behavior recursively.
837852

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:
853+
If the last argument is a multi-line closure with an explicit block,
854+
only apply the combining behavior if there are no other closure arguments.
842855

843856
```rust
857+
// Combinable
844858
foo(first_arg, x, |param| {
845859
action();
846860
foo(param)
847861
})
862+
// Not combinable, because the closure is not the last argument
863+
foo(
864+
first_arg,
865+
|param| {
866+
action();
867+
foo(param)
868+
},
869+
whatever,
870+
)
871+
// Not combinable, because the first line of the closure does not fit
872+
foo(
873+
first_arg,
874+
x,
875+
move |very_long_param_causing_line_to_overflow| -> Bar {
876+
action();
877+
foo(param)
878+
},
879+
)
880+
// Not combinable, because there is more than one closure argument
881+
foo(
882+
first_arg,
883+
|x| x.bar(),
884+
|param| {
885+
action();
886+
foo(param)
887+
},
888+
)
848889
```
849890

850891
## Ranges

0 commit comments

Comments
 (0)
Failed to load comments.