Skip to content

Commit a53d243

Browse files
authored
Unrolled build for rust-lang#119515
Rollup merge of rust-lang#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.
2 parents 030a12c + 163b1a6 commit a53d243

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/doc/style-guide/src/editions.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ include:
4343
- Miscellaneous `rustfmt` bugfixes.
4444
- Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order).
4545
- Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase".
46+
- Format single associated type `where` clauses on the same line if they fit.
4647

4748
## Rust 2015/2018/2021 style edition
4849

src/doc/style-guide/src/items.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,18 @@ Prefer to use single-letter names for generic parameters.
295295

296296
These rules apply for `where` clauses on any item.
297297

298-
If immediately following a closing bracket of any kind, write the keyword
299-
`where` on the same line, with a space before it.
298+
If a where clause is short, and appears on a short one-line function
299+
declaration with no body or on a short type with no `=`, format it on
300+
the same line as the declaration:
301+
302+
```rust
303+
fn new(&self) -> Self where Self: Sized;
304+
305+
type Item<'a>: SomeTrait where Self: 'a;
306+
```
307+
308+
Otherwise, if immediately following a closing bracket of any kind, write the
309+
keyword `where` on the same line, with a space before it.
300310

301311
Otherwise, put `where` on a new line at the same indentation level. Put each
302312
component of a `where` clause on its own line, block-indented. Use a trailing
@@ -347,7 +357,7 @@ where
347357
```
348358

349359
If a `where` clause is very short, prefer using an inline bound on the type
350-
parameter.
360+
parameter if possible.
351361

352362
If a component of a `where` clause does not fit and contains `+`, break it
353363
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
421431
bound, put a space after the colon but not before:
422432

423433
```rust
424-
pub type Foo: Bar;
434+
type Foo: Bar;
425435
```
426436

437+
If an associated type is short, has no `=`, and has a `where` clause with only
438+
one entry, format the entire type declaration including the `where` clause on
439+
the same line if it fits:
440+
441+
```rust
442+
type Item<'a> where Self: 'a;
443+
type Item<'a>: PartialEq + Send where Self: 'a;
444+
```
445+
446+
If the associated type has a `=`, or if the `where` clause contains multiple
447+
entries, format it across multiple lines as with a type alias.
448+
427449
## extern items
428450

429451
When writing extern items (such as `extern "C" fn`), always specify the ABI.

0 commit comments

Comments
 (0)