Contains over Filter is Empty

Prefer contains over using filter(where:).isEmpty

  • Identifier: contains_over_filter_is_empty
  • 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

let result = myList.filter(where: { $0 % 2 == 0 }).count > 1
let result = myList.filter { $0 % 2 == 0 }.count > 1
let result = myList.filter(where: { $0 % 2 == 0 }).count == 1
let result = myList.filter { $0 % 2 == 0 }.count == 1
let result = myList.filter(where: { $0 % 2 == 0 }).count != 1
let result = myList.filter { $0 % 2 == 0 }.count != 1
let result = myList.contains(where: { $0 % 2 == 0 })
let result = !myList.contains(where: { $0 % 2 == 0 })
let result = myList.contains(10)

Triggering Examples

let result = myList.filter(where: { $0 % 2 == 0 }).isEmpty
let result = !myList.filter(where: { $0 % 2 == 0 }).isEmpty
let result = myList.filter { $0 % 2 == 0 }.isEmpty
let result = myList.filter(where: someFunction).isEmpty