From 8c0fbcc9d89726090bcf4fbb756cd1edae90fd7e Mon Sep 17 00:00:00 2001 From: manuel-lopez Date: Fri, 24 Oct 2025 10:52:56 -0700 Subject: [PATCH 1/5] Add simplifyGenericConstraints SwiftFormat rule --- README.md | 38 +++++++++++++++++++ .../AirbnbSwiftFormatTool/airbnb.swiftformat | 1 + 2 files changed, 39 insertions(+) diff --git a/README.md b/README.md index a3dcfa0..38794f0 100644 --- a/README.md +++ b/README.md @@ -3275,6 +3275,44 @@ _You can enable the following settings in Xcode by running [this script](resourc +* (link) **Convert where clause generic constraints to inline angle bracket constraints for simple protocol conformances.** [![SwiftFormat: simplifyGenericConstraints](https://img.shields.io/badge/SwiftFormat-simplifyGenericConstraints-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#simplifyGenericConstraints) + +
+ + #### Why? + Inline generic constraints (``) are more concise and idiomatic than where clauses (` 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. + + ```swift + // WRONG + struct Spacecraft where T: Hashable, U: Codable {} + + class Mission where Payload: Equatable { + // ... + } + + enum Planet where Value: Decodable, Error: Swift.Error {} + + func launch(_ rocket: T) where T: Sendable {} + + // RIGHT + struct Spacecraft {} + + class Mission { + // ... + } + + enum Planet {} + + func launch(_ rocket: T) {} + + // ALSO RIGHT: Complex constraints remain in where clause + struct Galaxy where T.Element == Star {} + + func terraform(_ planet: T) where T: PlanetaryBody, T.Element == Moon {} + ``` + +
+ * (link) **Default type methods to `static`.**
diff --git a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat index eb683e4..3e3ebe3 100644 --- a/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat +++ b/Sources/AirbnbSwiftFormatTool/airbnb.swiftformat @@ -143,3 +143,4 @@ --rules redundantBreak --rules redundantTypedThrows --rules preferFinalClasses +--rules simplifyGenericConstraints \ No newline at end of file From 1646524236450a4dcfa399ec8cdef92e6085eee1 Mon Sep 17 00:00:00 2001 From: manuel-lopez Date: Fri, 24 Oct 2025 11:06:18 -0700 Subject: [PATCH 2/5] Simplify example --- README.md | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 38794f0..4c06098 100644 --- a/README.md +++ b/README.md @@ -3284,31 +3284,16 @@ _You can enable the following settings in Xcode by running [this script](resourc ```swift // WRONG - struct Spacecraft where T: Hashable, U: Codable {} - - class Mission where Payload: Equatable { - // ... + struct SpaceshipDashboard: View + where Left: View, Right: View + { + ... } - enum Planet where Value: Decodable, Error: Swift.Error {} - - func launch(_ rocket: T) where T: Sendable {} - // RIGHT - struct Spacecraft {} - - class Mission { - // ... + struct SpaceshipDashboard: View { + ... } - - enum Planet {} - - func launch(_ rocket: T) {} - - // ALSO RIGHT: Complex constraints remain in where clause - struct Galaxy where T.Element == Star {} - - func terraform(_ planet: T) where T: PlanetaryBody, T.Element == Moon {} ```
From c11a2cb8ecee92017d25a9b0701313a788f37073 Mon Sep 17 00:00:00 2001 From: manuel-lopez Date: Fri, 24 Oct 2025 11:08:22 -0700 Subject: [PATCH 3/5] Add func example --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4c06098..c498be3 100644 --- a/README.md +++ b/README.md @@ -3290,10 +3290,17 @@ _You can enable the following settings in Xcode by running [this script](resourc ... } + func launch(_ rocket: T) where T: Sendable {} + // RIGHT struct SpaceshipDashboard: View { ... } + + func launch(_ rocket: T) {} + + // ALSO RIGHT: Complex constraints remain in where clause + struct Galaxy where T.Element == Star {} ``` From cabdb99830b196769164ddf2a190649c065e37ab Mon Sep 17 00:00:00 2001 From: manuel-lopez Date: Fri, 24 Oct 2025 11:11:41 -0700 Subject: [PATCH 4/5] Update blurb --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c498be3..bd0d186 100644 --- a/README.md +++ b/README.md @@ -3275,7 +3275,7 @@ _You can enable the following settings in Xcode by running [this script](resourc -* (link) **Convert where clause generic constraints to inline angle bracket constraints for simple protocol conformances.** [![SwiftFormat: simplifyGenericConstraints](https://img.shields.io/badge/SwiftFormat-simplifyGenericConstraints-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#simplifyGenericConstraints) +* (link) **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)
From 8af24936470a1e03b5e83bdec2644b6cdbdee7af Mon Sep 17 00:00:00 2001 From: manuel-lopez Date: Tue, 28 Oct 2025 17:13:30 -0700 Subject: [PATCH 5/5] Update func example --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd0d186..6df1cb0 100644 --- a/README.md +++ b/README.md @@ -3290,14 +3290,28 @@ _You can enable the following settings in Xcode by running [this script](resourc ... } - func launch(_ rocket: T) where T: Sendable {} + extension Spaceship { + func fly( + to: Destination, + didArrive: (Destination) -> Void + ) where Destination: PlanetaryBody { + ... + } + } // RIGHT struct SpaceshipDashboard: View { ... } - func launch(_ rocket: T) {} + extension Spaceship { + func fly( + to: Destination, + didArrive: (Destination) -> Void + ) { + ... + } + } // ALSO RIGHT: Complex constraints remain in where clause struct Galaxy where T.Element == Star {}