Reduce Boolean
Prefer using .allSatisfy()
or .contains()
over reduce(true)
or reduce(false)
.
- Identifier:
reduce_boolean
- Enabled by default: Yes
- Supports autocorrection: No
- Kind: performance
- Analyzer rule: No
- Minimum Swift compiler version: 5.0.0
- Default configuration:
Key Value severity warning
Non Triggering Examples
nums.reduce(0) { $0.0 + $0.1 }
nums.reduce(0.0) { $0.0 + $0.1 }
nums.reduce(initial: true) { $0.0 && $0.1 == 3 }
Triggering Examples
let allNines = nums.↓reduce(true) { $0.0 && $0.1 == 9 }
let anyNines = nums.↓reduce(false) { $0.0 || $0.1 == 9 }
let allValid = validators.↓reduce(true) { $0 && $1(input) }
let anyValid = validators.↓reduce(false) { $0 || $1(input) }
let allNines = nums.↓reduce(true, { $0.0 && $0.1 == 9 })
let anyNines = nums.↓reduce(false, { $0.0 || $0.1 == 9 })
let allValid = validators.↓reduce(true, { $0 && $1(input) })
let anyValid = validators.↓reduce(false, { $0 || $1(input) })
nums.reduce(into: true) { (r: inout Bool, s) in r = r && (s == 3) }