Skip to content

Commit

Permalink
Add rule to prefer count(where: { ... }) over filter { ... }.count
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Dec 9, 2024
1 parent 25cc395 commit f60fa7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,21 @@ _You can enable the following settings in Xcode by running [this script](resourc
```
</details>

* <a id='count-where'></a>(<a href='#count-where'>link</a>) **Prefer `count(where: { })` over `filter { }.count`**.

<details>

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.

```swift
// WRONG
let planetsWithMoons = planets.filter { !$0.moons.isEmpty }.count

// RIGHT
let planetsWithMoons = planets.count(where: { !$0.moons.isEmpty })
```
</details>

**[ back to top](#table-of-contents)**

## File Organization
Expand Down
1 change: 1 addition & 0 deletions Sources/AirbnbSwiftFormatTool/airbnb.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@
--rules blankLinesBetweenChainedFunctions
--rules unusedPrivateDeclaration
--rules emptyExtension
--rules preferCountWhere

0 comments on commit f60fa7d

Please sign in to comment.