Skip to content

Commit 9f5bc5c

Browse files
authored
Add rule to convert where clause generic constraints to inline angle bracket constraints for simple protocol conformances (#335)
#### Summary This PR adds a new `simplifyGenericConstraints` rule that converts where clause generic constraints to inline angle bracket constraints for simple protocol conformances. ```swift // WRONG struct SpaceshipDashboard<Left, Right>: View where Left: View, Right: View { ... } extension Spaceship { func fly<Destination: PlanetaryBody>( to: Destination, didArrive: (Destination) -> Void ) { ... } } // RIGHT struct SpaceshipDashboard<Left: View, Right: View>: View { ... } extension Spaceship { func fly<Destination: PlanetaryBody>( to: Destination, didArrive: (Destination) -> Void ) { ... } } // ALSO RIGHT: Complex constraints remain in where clause struct Galaxy<T: Collection> where T.Element == Star {} ``` ### Reasoning - Inline generic constraints (<T: Protocol>) are more concise and idiomatic than where clauses (<T> where T: Protocol) for simple protocol conformances. - Using inline constraints for simple cases makes generic declarations easier to read at a glance. - Where clauses are reserved for complex constraints that cannot be expressed inline, like associated type constraints (T.Element == Star) or concrete type equality. - Multiple protocol conformances on the same type are automatically combined using & (e.g., <T: Hashable & Codable>).
1 parent 5fdb6f7 commit 9f5bc5c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,50 @@ _You can enable the following settings in Xcode by running [this script](resourc
32753275

32763276
</details>
32773277

3278+
* <a id='simplify-generic-constraints'></a>(<a href='#simplify-generic-constraints'>link</a>) **Prefer defining simple generic constraints in the generic parameter list rather than in the where clause.** [![SwiftFormat: simplifyGenericConstraints](https://img.shields.io/badge/SwiftFormat-simplifyGenericConstraints-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#simplifyGenericConstraints)
3279+
3280+
<details>
3281+
3282+
#### Why?
3283+
Inline generic constraints (`<T: Protocol>`) are more concise and idiomatic than where clauses (`<T> where T: Protocol`) for simple protocol conformances. Using inline constraints for simple cases makes generic declarations easier to read at a glance. Where clauses are reserved for complex constraints that cannot be expressed inline, like associated type constraints (`T.Element == Star`) or concrete type equality.
3284+
3285+
```swift
3286+
// WRONG
3287+
struct SpaceshipDashboard<Left, Right>: View
3288+
where Left: View, Right: View
3289+
{
3290+
...
3291+
}
3292+
3293+
extension Spaceship {
3294+
func fly<Destination>(
3295+
to: Destination,
3296+
didArrive: (Destination) -> Void
3297+
) where Destination: PlanetaryBody {
3298+
...
3299+
}
3300+
}
3301+
3302+
// RIGHT
3303+
struct SpaceshipDashboard<Left: View, Right: View>: View {
3304+
...
3305+
}
3306+
3307+
extension Spaceship {
3308+
func fly<Destination: PlanetaryBody>(
3309+
to: Destination,
3310+
didArrive: (Destination) -> Void
3311+
) {
3312+
...
3313+
}
3314+
}
3315+
3316+
// ALSO RIGHT: Complex constraints remain in where clause
3317+
struct Galaxy<T: Collection> where T.Element == Star {}
3318+
```
3319+
3320+
</details>
3321+
32783322
* <a id='static-type-methods-by-default'></a>(<a href='#static-type-methods-by-default'>link</a>) **Default type methods to `static`.**
32793323

32803324
<details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,4 @@
144144
--rules redundantBreak
145145
--rules redundantTypedThrows
146146
--rules preferFinalClasses
147+
--rules simplifyGenericConstraints

0 commit comments

Comments
 (0)