[style 2024] Combine all last arg delimited exprs
This commit is contained in:
parent
1e836d12d3
commit
bed0c9d97f
@ -36,6 +36,10 @@ For a full history of changes in the Rust 2024 style edition, see the git
|
|||||||
history of the style guide. Notable changes in the Rust 2024 style edition
|
history of the style guide. Notable changes in the Rust 2024 style edition
|
||||||
include:
|
include:
|
||||||
|
|
||||||
|
- [#114764](https://github.com/rust-lang/rust/pull/114764) As the last member
|
||||||
|
of a delimited expression, delimited expressions are generally combinable,
|
||||||
|
regardless of the number of members. Previously only applied with exactly
|
||||||
|
one member (except for closures with explicit blocks).
|
||||||
- Miscellaneous `rustfmt` bugfixes.
|
- Miscellaneous `rustfmt` bugfixes.
|
||||||
|
|
||||||
## Rust 2015/2018/2021 style edition
|
## Rust 2015/2018/2021 style edition
|
||||||
|
@ -801,11 +801,11 @@ E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
|
|||||||
|
|
||||||
## Combinable expressions
|
## Combinable expressions
|
||||||
|
|
||||||
Where a function call has a single argument, and that argument is formatted
|
When the last argument in a function call is formatted across
|
||||||
across multiple-lines, format the outer call as if it were a single-line call,
|
multiple-lines, format the outer call as if it were a single-line call,
|
||||||
if the result fits. Apply the same combining behaviour to any similar
|
if the result fits. Apply the same combining behaviour to any similar
|
||||||
expressions which have multi-line, block-indented lists of sub-expressions
|
expressions which have multi-line, block-indented lists of sub-expressions
|
||||||
delimited by parentheses (e.g., macros or tuple struct literals). E.g.,
|
delimited by parentheses, brackets, or braces. E.g.,
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
foo(bar(
|
foo(bar(
|
||||||
@ -831,20 +831,61 @@ let arr = [combinable(
|
|||||||
an_expr,
|
an_expr,
|
||||||
another_expr,
|
another_expr,
|
||||||
)];
|
)];
|
||||||
|
|
||||||
|
let x = Thing(an_expr, another_expr, match cond {
|
||||||
|
A => 1,
|
||||||
|
B => 2,
|
||||||
|
});
|
||||||
|
|
||||||
|
let x = format!("Stuff: {}", [
|
||||||
|
an_expr,
|
||||||
|
another_expr,
|
||||||
|
]);
|
||||||
|
|
||||||
|
let x = func(an_expr, another_expr, SomeStruct {
|
||||||
|
field: this_is_long,
|
||||||
|
another_field: 123,
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Apply this behavior recursively.
|
Apply this behavior recursively.
|
||||||
|
|
||||||
For a function with multiple arguments, if the last argument is a multi-line
|
If the last argument is a multi-line closure with an explicit block,
|
||||||
closure with an explicit block, there are no other closure arguments, and all
|
only apply the combining behavior if there are no other closure arguments.
|
||||||
the arguments and the first line of the closure fit on the first line, use the
|
|
||||||
same combining behavior:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
// Combinable
|
||||||
foo(first_arg, x, |param| {
|
foo(first_arg, x, |param| {
|
||||||
action();
|
action();
|
||||||
foo(param)
|
foo(param)
|
||||||
})
|
})
|
||||||
|
// Not combinable, because the closure is not the last argument
|
||||||
|
foo(
|
||||||
|
first_arg,
|
||||||
|
|param| {
|
||||||
|
action();
|
||||||
|
foo(param)
|
||||||
|
},
|
||||||
|
whatever,
|
||||||
|
)
|
||||||
|
// Not combinable, because the first line of the closure does not fit
|
||||||
|
foo(
|
||||||
|
first_arg,
|
||||||
|
x,
|
||||||
|
move |very_long_param_causing_line_to_overflow| -> Bar {
|
||||||
|
action();
|
||||||
|
foo(param)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
// Not combinable, because there is more than one closure argument
|
||||||
|
foo(
|
||||||
|
first_arg,
|
||||||
|
|x| x.bar(),
|
||||||
|
|param| {
|
||||||
|
action();
|
||||||
|
foo(param)
|
||||||
|
},
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ranges
|
## Ranges
|
||||||
|
Loading…
x
Reference in New Issue
Block a user