Skip to content

Commit

Permalink
Merge pull request #2638 from realm/cg-first_where-fix
Browse files Browse the repository at this point in the history
Fix false positives on rule `first_where`
  • Loading branch information
Jeehut authored Feb 27, 2019
2 parents 1b0ac66 + d9113a7 commit 7155b86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
[Cihat Gündüz](https://github.com/Dschee)
[#2437](https://github.com/realm/SwiftLint/issues/2437)

* Fix `first_where` for some calls on Realm collection types.
[Cihat Gündüz](https://github.com/Dschee)
[#1930](https://github.com/realm/SwiftLint/issues/1930)

## 0.30.1: Localized Stain Remover

#### Breaking
Expand Down
8 changes: 8 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -7591,6 +7591,14 @@ match(pattern: pattern).filter { $0.first == .identifier }
collection.filter("stringCol = '3'").first
```

```swift
realm?.objects(User.self).filter(NSPredicate(format: "email ==[c] %@", email)).first
```

```swift
if let pause = timeTracker.pauses.filter("beginDate < %@", beginDate).first { print(pause) }
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
22 changes: 15 additions & 7 deletions Source/SwiftLintFramework/Rules/Performance/FirstWhereRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public struct FirstWhereRule: CallPairRule, OptInRule, ConfigurationProviderRule
"myList.first(where: { $0 % 2 == 0 })\n",
"match(pattern: pattern).filter { $0.first == .identifier }\n",
"(myList.filter { $0 == 1 }.suffix(2)).first\n",
"collection.filter(\"stringCol = '3'\").first"
"collection.filter(\"stringCol = '3'\").first",
"realm?.objects(User.self).filter(NSPredicate(format: \"email ==[c] %@\", email)).first",
"if let pause = timeTracker.pauses.filter(\"beginDate < %@\", beginDate).first { print(pause) }"
],
triggeringExamples: [
"↓myList.filter { $0 % 2 == 0 }.first\n",
Expand All @@ -30,12 +32,18 @@ public struct FirstWhereRule: CallPairRule, OptInRule, ConfigurationProviderRule
)

public func validate(file: File) -> [StyleViolation] {
return validate(file: file,
pattern: "[\\}\\)]\\s*\\.first",
patternSyntaxKinds: [.identifier],
callNameSuffix: ".filter",
severity: configuration.severity) { dictionary in
if !dictionary.substructure.isEmpty {
return validate(
file: file,
pattern: "[\\}\\)]\\s*\\.first",
patternSyntaxKinds: [.identifier],
callNameSuffix: ".filter",
severity: configuration.severity
) { dictionary in
if
!dictionary.substructure.isEmpty &&
dictionary.substructure.last?.kind.flatMap(SwiftExpressionKind.init) != .argument &&
dictionary.substructure.last?.name != "NSPredicate"
{
return true // has a substructure, like a closure
}

Expand Down

0 comments on commit 7155b86

Please sign in to comment.