Rollup merge of #119515 - joshtriplett:style-guide-gat-where-clause-same-line, r=compiler-errors

style-guide: Format single associated type `where` clauses on the same line

In particular, lifetime-generic associated types often have a
`where Self: 'a` bound, which we can format on the same line.
This commit is contained in:
Matthias Krüger 2024-05-13 20:29:17 +02:00 committed by GitHub
commit 4d1cce9de5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -43,6 +43,7 @@ include:
- 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".
- Format single associated type `where` clauses on the same line if they fit.
## Rust 2015/2018/2021 style edition ## Rust 2015/2018/2021 style edition

View File

@ -295,8 +295,18 @@ Prefer to use single-letter names for generic parameters.
These rules apply for `where` clauses on any item. These rules apply for `where` clauses on any item.
If immediately following a closing bracket of any kind, write the keyword If a where clause is short, and appears on a short one-line function
`where` on the same line, with a space before it. declaration with no body or on a short type with no `=`, format it on
the same line as the declaration:
```rust
fn new(&self) -> Self where Self: Sized;
type Item<'a>: SomeTrait where Self: 'a;
```
Otherwise, if immediately following a closing bracket of any kind, write the
keyword `where` on the same line, with a space before it.
Otherwise, put `where` on a new line at the same indentation level. Put each Otherwise, put `where` on a new line at the same indentation level. Put each
component of a `where` clause on its own line, block-indented. Use a trailing component of a `where` clause on its own line, block-indented. Use a trailing
@ -347,7 +357,7 @@ where
``` ```
If a `where` clause is very short, prefer using an inline bound on the type If a `where` clause is very short, prefer using an inline bound on the type
parameter. parameter if possible.
If a component of a `where` clause does not fit and contains `+`, break it If a component of a `where` clause does not fit and contains `+`, break it
before each `+` and block-indent the continuation lines. Put each bound on its before each `+` and block-indent the continuation lines. Put each bound on its
@ -421,9 +431,21 @@ Format associated types like type aliases. Where an associated type has a
bound, put a space after the colon but not before: bound, put a space after the colon but not before:
```rust ```rust
pub type Foo: Bar; type Foo: Bar;
``` ```
If an associated type is short, has no `=`, and has a `where` clause with only
one entry, format the entire type declaration including the `where` clause on
the same line if it fits:
```rust
type Item<'a> where Self: 'a;
type Item<'a>: PartialEq + Send where Self: 'a;
```
If the associated type has a `=`, or if the `where` clause contains multiple
entries, format it across multiple lines as with a type alias.
## extern items ## extern items
When writing extern items (such as `extern "C" fn`), always specify the ABI. When writing extern items (such as `extern "C" fn`), always specify the ABI.