-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[style edition 2024] Combine all delimited exprs as last argument #114764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -801,11 +801,11 @@ E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not. | |
|
||
## Combinable expressions | ||
|
||
Where a function call has a single argument, and that argument is formatted | ||
across multiple-lines, format the outer call as if it were a single-line call, | ||
When the last argument in a function call is formatted across | ||
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 | ||
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 | ||
foo(bar( | ||
|
@@ -831,20 +831,61 @@ let arr = [combinable( | |
an_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. | ||
|
||
For a function with multiple arguments, if the last argument is a multi-line | ||
closure with an explicit block, there are no other closure arguments, and all | ||
the arguments and the first line of the closure fit on the first line, use the | ||
same combining behavior: | ||
If the last argument is a multi-line closure with an explicit block, | ||
only apply the combining behavior if there are no other closure arguments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to keep this property? Here's the last example from this section, reformatted without this restriction: foo(first_arg, |x| x.bar(), |param| {
action();
foo(param)
}) We might want to prohibit this formatting, but let's consider this case specifically before assuming we want to preserve this restriction. Based on this formatting, I do think we should keep this restriction, to make it easier to read the separate closures. |
||
|
||
```rust | ||
// Combinable | ||
foo(first_arg, x, |param| { | ||
action(); | ||
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 | ||
|
Uh oh!
There was an error while loading. Please reload this page.