Skip to content

Commit

Permalink
Merge pull request #2701 from r-plus/bugfix-2700
Browse files Browse the repository at this point in the history
fix: false positive on sorted_first_last with firstIndex(of:), firstIndex(where:), lastIndex(of:) and lastIndex(where:) method
  • Loading branch information
marcelofabri committed Apr 2, 2019
2 parents 800ca0e + 72dc428 commit 1d7b37b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
[Dalton Claybrook](https://github.com/daltonclaybrook)
[#2683](https://github.com/realm/SwiftLint/issues/2683)

* Fix false positives on `sorted_first_last` when calling `firstIndex` and
`lastIndex` method.
[Taiki Komaba](https://github.com/r-plus)
[#2700](https://github.com/realm/SwiftLint/issues/2700)

## 0.31.0: Busy Laundromat

#### Breaking
Expand Down
24 changes: 24 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -17577,6 +17577,30 @@ let message = messages.sorted(byKeyPath: #keyPath(Message.timestamp)).last
let message = messages.sorted(byKeyPath: "timestamp", ascending: false).first
```

```swift
myList.sorted().firstIndex(of: key)
```

```swift
myList.sorted().lastIndex(of: key)
```

```swift
myList.sorted().firstIndex(where: someFunction)
```

```swift
myList.sorted().lastIndex(where: someFunction)
```

```swift
myList.sorted().firstIndex { $0 == key }
```

```swift
myList.sorted().lastIndex { $0 == key }
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public struct SortedFirstLastRule: CallPairRule, OptInRule, ConfigurationProvide
"let max = myList.max()\n",
"let max = myList.max(by: { $0 < $1 })\n",
"let message = messages.sorted(byKeyPath: #keyPath(Message.timestamp)).last",
"let message = messages.sorted(byKeyPath: \"timestamp\", ascending: false).first"
"let message = messages.sorted(byKeyPath: \"timestamp\", ascending: false).first",
"myList.sorted().firstIndex(of: key)",
"myList.sorted().lastIndex(of: key)",
"myList.sorted().firstIndex(where: someFunction)",
"myList.sorted().lastIndex(where: someFunction)",
"myList.sorted().firstIndex { $0 == key }",
"myList.sorted().lastIndex { $0 == key }"
],
triggeringExamples: [
"↓myList.sorted().first\n",
Expand All @@ -38,7 +44,7 @@ public struct SortedFirstLastRule: CallPairRule, OptInRule, ConfigurationProvide

public func validate(file: File) -> [StyleViolation] {
return validate(file: file,
pattern: "[\\}\\)]\\s*\\.(first|last)",
pattern: "[\\}\\)]\\s*\\.(first|last)(?!Index)",
patternSyntaxKinds: [.identifier],
callNameSuffix: ".sorted",
severity: configuration.severity) { dictionary in
Expand Down

0 comments on commit 1d7b37b

Please sign in to comment.