First Where

Prefer using .first(where:) over .filter { }.first in collections

  • Identifier: first_where
  • Enabled by default: No
  • Supports autocorrection: No
  • Kind: performance
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity warning

Non Triggering Examples

kinds.filter(excludingKinds.contains).isEmpty && kinds.first == .identifier
myList.first(where: { $0 % 2 == 0 })
match(pattern: pattern).filter { $0.first == .identifier }
(myList.filter { $0 == 1 }.suffix(2)).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) }

Triggering Examples

myList.filter { $0 % 2 == 0 }.first
myList.filter({ $0 % 2 == 0 }).first
myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first
myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first?.something()
myList.filter(someFunction).first
myList.filter({ $0 % 2 == 0 })
.first
(myList.filter { $0 == 1 }).first
myListOfDict.filter { dict in dict["1"] }.first
myListOfDict.filter { $0["someString"] }.first