Contains over Filter Count

Prefer contains over comparing filter(where:).count to 0

  • Identifier: contains_over_filter_count
  • 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 > 01
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 == 01
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 != 01
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 }).count > 0
let result = myList.filter { $0 % 2 == 0 }.count > 0
let result = myList.filter(where: someFunction).count > 0
let result = myList.filter(where: { $0 % 2 == 0 }).count == 0
let result = myList.filter { $0 % 2 == 0 }.count == 0
let result = myList.filter(where: someFunction).count == 0
let result = myList.filter(where: { $0 % 2 == 0 }).count != 0
let result = myList.filter { $0 % 2 == 0 }.count != 0
let result = myList.filter(where: someFunction).count != 0