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 authored Dec 12, 2024
1 parent 0192e7e commit 3f6adc3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ let package = Package(

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

.binaryTarget(
name: "SwiftLintBinary",
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3809,6 +3809,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 using `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 unusedPrivateDeclarations
--rules emptyExtensions
--rules preferCountWhere

0 comments on commit 3f6adc3

Please sign in to comment.