Skip to content

Commit

Permalink
Check first argument label in reduce_boolean rule (realm#4895)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Apr 16, 2023
1 parent 97fd216 commit 5aed7ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"),
Expand All @@ -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) }")
]
)

Expand All @@ -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
Expand Down

0 comments on commit 5aed7ce

Please sign in to comment.