Last Where

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

  • Identifier: last_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.last == .identifier
myList.last(where: { $0 % 2 == 0 })
match(pattern: pattern).filter { $0.last == .identifier }
(myList.filter { $0 == 1 }.suffix(2)).last
collection.filter("stringCol = '3'").last

Triggering Examples

myList.filter { $0 % 2 == 0 }.last
myList.filter({ $0 % 2 == 0 }).last
myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).last
myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).last?.something()
myList.filter(someFunction).last
myList.filter({ $0 % 2 == 0 })
.last
(myList.filter { $0 == 1 }).last