Skip to content

Commit 3f6adc3

Browse files
authored
Add rule to prefer count(where: { ... }) over filter { ... }.count (#295)
1 parent 0192e7e commit 3f6adc3

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ let package = Package(
4242

4343
.binaryTarget(
4444
name: "swiftformat",
45-
url: "https://github.com/calda/SwiftFormat/releases/download/0.56-beta-1-build-2/SwiftFormat.artifactbundle.zip",
46-
checksum: "52555bc9fa90a31e68f77917eac2307c2ebbad023d83b3e728e0deb2c7ec98a2"),
45+
url: "https://github.com/calda/SwiftFormat/releases/download/0.56-beta-3/SwiftFormat.artifactbundle.zip",
46+
checksum: "4fef0f8d76ed185c1fe2026cfd6252504b1adb37fdda4ffff379fa2820d282cd"),
4747

4848
.binaryTarget(
4949
name: "SwiftLintBinary",

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,6 +3809,21 @@ _You can enable the following settings in Xcode by running [this script](resourc
38093809
```
38103810
</details>
38113811

3812+
* <a id='count-where'></a>(<a href='#count-where'>link</a>) **Prefer using `count(where: { })` over `filter { }.count`**.
3813+
3814+
<details>
3815+
3816+
Swift 6.0 ([finally!](https://forums.swift.org/t/accepted-again-se-0220-count-where/66659)) added a `count(where:)` method to the standard library. Prefer using the `count(where:)` method over using the `filter(_:)` method followed by a `count` call.
3817+
3818+
```swift
3819+
// WRONG
3820+
let planetsWithMoons = planets.filter { !$0.moons.isEmpty }.count
3821+
3822+
// RIGHT
3823+
let planetsWithMoons = planets.count(where: { !$0.moons.isEmpty })
3824+
```
3825+
</details>
3826+
38123827
**[ back to top](#table-of-contents)**
38133828

38143829
## File Organization

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@
121121
--rules blankLinesBetweenChainedFunctions
122122
--rules unusedPrivateDeclarations
123123
--rules emptyExtensions
124+
--rules preferCountWhere

0 commit comments

Comments
 (0)