Commit 9f5bc5c
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
2 files changed
+45
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3275 | 3275 | | |
3276 | 3276 | | |
3277 | 3277 | | |
| 3278 | + | |
| 3279 | + | |
| 3280 | + | |
| 3281 | + | |
| 3282 | + | |
| 3283 | + | |
| 3284 | + | |
| 3285 | + | |
| 3286 | + | |
| 3287 | + | |
| 3288 | + | |
| 3289 | + | |
| 3290 | + | |
| 3291 | + | |
| 3292 | + | |
| 3293 | + | |
| 3294 | + | |
| 3295 | + | |
| 3296 | + | |
| 3297 | + | |
| 3298 | + | |
| 3299 | + | |
| 3300 | + | |
| 3301 | + | |
| 3302 | + | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
| 3314 | + | |
| 3315 | + | |
| 3316 | + | |
| 3317 | + | |
| 3318 | + | |
| 3319 | + | |
| 3320 | + | |
| 3321 | + | |
3278 | 3322 | | |
3279 | 3323 | | |
3280 | 3324 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
0 commit comments