Rollup merge of #119838 - joshtriplett:style-guide-binop-indent, r=compiler-errors

style-guide: When breaking binops handle multi-line first operand better

Use the indentation of the *last* line of the first operand, not the first.

Fixes https://github.com/rust-lang/style-team/issues/189
This commit is contained in:
Michael Goulet 2024-05-14 09:55:27 -04:00 committed by GitHub
commit 36287830a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View File

@ -40,6 +40,8 @@ include:
of a delimited expression, delimited expressions are generally combinable, of a delimited expression, delimited expressions are generally combinable,
regardless of the number of members. Previously only applied with exactly regardless of the number of members. Previously only applied with exactly
one member (except for closures with explicit blocks). one member (except for closures with explicit blocks).
- When line-breaking a binary operator, if the first operand spans multiple
lines, use the base indentation of the last line.
- Miscellaneous `rustfmt` bugfixes. - Miscellaneous `rustfmt` bugfixes.
- Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order). - Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order).
- Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase". - Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase".

View File

@ -328,6 +328,37 @@ foo_bar
Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather Prefer line-breaking at an assignment operator (either `=` or `+=`, etc.) rather
than at other binary operators. than at other binary operators.
If line-breaking at a binary operator (including assignment operators) where the
first operand spans multiple lines, use the base indentation of the *last*
line of the first operand, and indent relative to that:
```rust
impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info
+ long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}
```
### Casts (`as`) ### Casts (`as`)
Format `as` casts like a binary operator. In particular, always include spaces Format `as` casts like a binary operator. In particular, always include spaces