From 5aed7ce95cb59bb5046b5e41c6c238d5621496df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Sun, 16 Apr 2023 09:58:27 +0200 Subject: [PATCH] Check first argument label in `reduce_boolean` rule (#4895) --- CHANGELOG.md | 5 +++++ .../Rules/Performance/ReduceBooleanRule.swift | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fee68245a..c7a22140d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Source/SwiftLintFramework/Rules/Performance/ReduceBooleanRule.swift b/Source/SwiftLintFramework/Rules/Performance/ReduceBooleanRule.swift index 73a39efaef..4c2da4f89e 100644 --- a/Source/SwiftLintFramework/Rules/Performance/ReduceBooleanRule.swift +++ b/Source/SwiftLintFramework/Rules/Performance/ReduceBooleanRule.swift @@ -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