Contains over First not Nil
Prefer contains
over first(where:) != nil
and firstIndex(where:) != nil
.
- Identifier:
contains_over_first_not_nil
- Enabled by default: No
- Supports autocorrection: No
- Kind: performance
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
let first = myList.first(where: { $0 % 2 == 0 })
let first = myList.first { $0 % 2 == 0 }
let firstIndex = myList.firstIndex(where: { $0 % 2 == 0 })
let firstIndex = myList.firstIndex { $0 % 2 == 0 }
Triggering Examples
↓myList.first { $0 % 2 == 0 } != nil
↓myList.first(where: { $0 % 2 == 0 }) != nil
↓myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) != nil
↓myList.first(where: someFunction) != nil
↓myList.map { $0 + 1 }.first { $0 % 2 == 0 } != nil
(↓myList.first { $0 % 2 == 0 }) != nil
↓myList.first { $0 % 2 == 0 } == nil
↓myList.first(where: { $0 % 2 == 0 }) == nil
↓myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) == nil
↓myList.first(where: someFunction) == nil
↓myList.map { $0 + 1 }.first { $0 % 2 == 0 } == nil
(↓myList.first { $0 % 2 == 0 }) == nil
↓myList.firstIndex { $0 % 2 == 0 } != nil
↓myList.firstIndex(where: { $0 % 2 == 0 }) != nil
↓myList.map { $0 + 1 }.firstIndex(where: { $0 % 2 == 0 }) != nil
↓myList.firstIndex(where: someFunction) != nil
↓myList.map { $0 + 1 }.firstIndex { $0 % 2 == 0 } != nil
(↓myList.firstIndex { $0 % 2 == 0 }) != nil
↓myList.firstIndex { $0 % 2 == 0 } == nil
↓myList.firstIndex(where: { $0 % 2 == 0 }) == nil
↓myList.map { $0 + 1 }.firstIndex(where: { $0 % 2 == 0 }) == nil
↓myList.firstIndex(where: someFunction) == nil
↓myList.map { $0 + 1 }.firstIndex { $0 % 2 == 0 } == nil
(↓myList.firstIndex { $0 % 2 == 0 }) == nil