Check first argument label in `reduce_boolean` rule (#4895)

This commit is contained in:
Danny Mösch 2023-04-16 09:58:27 +02:00 committed by GitHub
parent 97fd216455
commit 5aed7ce95c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -49,6 +49,11 @@
* Fix false positives in `indentation_width` rule.
[Sven Münnich](https://github.com/svenmuennich)
* Do not trigger `reduce_boolean` on `reduce` methods with a first named
argument that is different from `into`.
[SimplyDanny](https://github.com/SimplyDanny)
[#4894](https://github.com/realm/SwiftLint/issues/4894)
## 0.51.0: bzllint
#### Breaking

View File

@ -12,7 +12,8 @@ struct ReduceBooleanRule: SwiftSyntaxRule, ConfigurationProviderRule {
kind: .performance,
nonTriggeringExamples: [
Example("nums.reduce(0) { $0.0 + $0.1 }"),
Example("nums.reduce(0.0) { $0.0 + $0.1 }")
Example("nums.reduce(0.0) { $0.0 + $0.1 }"),
Example("nums.reduce(initial: true) { $0.0 && $0.1 == 3 }")
],
triggeringExamples: [
Example("let allNines = nums.↓reduce(true) { $0.0 && $0.1 == 9 }"),
@ -22,7 +23,8 @@ struct ReduceBooleanRule: SwiftSyntaxRule, ConfigurationProviderRule {
Example("let allNines = nums.↓reduce(true, { $0.0 && $0.1 == 9 })"),
Example("let anyNines = nums.↓reduce(false, { $0.0 || $0.1 == 9 })"),
Example("let allValid = validators.↓reduce(true, { $0 && $1(input) })"),
Example("let anyValid = validators.↓reduce(false, { $0 || $1(input) })")
Example("let anyValid = validators.↓reduce(false, { $0 || $1(input) })"),
Example("nums.reduce(into: true) { (r: inout Bool, s) in r = r && (s == 3) }")
]
)
@ -38,6 +40,7 @@ private extension ReduceBooleanRule {
let calledExpression = node.calledExpression.as(MemberAccessExprSyntax.self),
calledExpression.name.text == "reduce",
let firstArgument = node.argumentList.first,
firstArgument.label?.text ?? "into" == "into",
let bool = firstArgument.expression.as(BooleanLiteralExprSyntax.self)
else {
return