Skip to content

Add 'Reduce Into Instead Of Loop' rule #6078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2b0462a
Implement ReduceIntoInsteadOfLoop rule
snofla Jun 18, 2023
abf8ea9
Cleanups
snofla Jun 18, 2023
9d90eba
Fix build
snofla Jun 19, 2023
437da08
Move models to separate file
snofla Jun 19, 2023
31363d4
Move couple of extension helper functions
snofla Jun 19, 2023
4f0e6f8
Merge commit 'ad23d08cdaa7d656a6e96d6f6cc7b0d165f0a8f2'
snofla May 10, 2025
3746b58
Implement ReduceIntoInsteadOfLoop rule
snofla Jun 18, 2023
63bed40
Cleanups
snofla Jun 18, 2023
810a508
Fix build
snofla Jun 19, 2023
a4f3b78
Move models to separate file
snofla Jun 19, 2023
4df2cb9
Move couple of extension helper functions
snofla Jun 19, 2023
06cf616
Make it work with new SwiftSyntax
snofla May 11, 2025
74a4372
Rename
snofla May 11, 2025
5016d85
Fix new SwiftSyntax forStmt
snofla May 11, 2025
86560c5
Fix new SwiftSyntax-isms
snofla May 12, 2025
27c7188
Clean up helpers
snofla May 12, 2025
7ff5b30
Support array initialisation expression
snofla May 12, 2025
40c4ff6
Regenerate built-in rules to include our rule
snofla May 12, 2025
6822901
Handle assignment expression `varName = <...>`
snofla May 12, 2025
b92bce3
Clean up
snofla May 12, 2025
5ea7e80
Clean up
snofla May 12, 2025
f91b8e9
Add triggering/non-triggering examples for the unit tests
snofla May 12, 2025
1443cb7
Auto applied fixes
snofla May 12, 2025
4a67268
Make opt-in
snofla May 12, 2025
e773996
Add credit note
snofla May 12, 2025
a42d66b
Fix ChangeLog line len
snofla May 12, 2025
9e8fbbc
Add extra trailing space
snofla May 12, 2025
6ef9351
Merge pull request #2 from snofla/snofla/use-reduce-into-1
snofla May 12, 2025
ca19f7d
Remove old files
snofla May 12, 2025
5543177
Merge pull request #3 from snofla/snofla/use-reduce-into-2
snofla May 12, 2025
21446f1
Clean up merge warnings
snofla May 13, 2025
81175c1
Merge pull request #4 from snofla/snofla/use-reduce-into-2
snofla May 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Auto applied fixes
  • Loading branch information
snofla committed May 12, 2025
commit 1443cb7dc9fe87228c2a24faf66c950e9352e1d9
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftSyntax
typealias ReferencedVariable = ReduceIntoInsteadOfLoopRule.ReferencedVariable
typealias CollectionType = ReduceIntoInsteadOfLoopRule.CollectionType

@SwiftSyntaxRule
@SwiftSyntaxRule(optIn: true)
struct ReduceIntoInsteadOfLoopRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

Expand All @@ -28,7 +28,7 @@ internal extension ReduceIntoInsteadOfLoopRule {
let selected = all.reduce(into: [ForStmtSyntax: [VariableDeclSyntax]]()) { partialResult, element in
// we're interested fully type declared and implicitly declared by initializer
let interestingVariableDecls = element.value.filter { variableDecl in
return variableDecl.isTypeAnnotatedAndInitializer
variableDecl.isTypeAnnotatedAndInitializer
|| variableDecl.isCollectionTypeInitializer
}
guard !interestingVariableDecls.isEmpty else {
Expand Down Expand Up @@ -142,7 +142,7 @@ private extension VariableDeclSyntax {
return false
}
guard let initializerClausePatternBinding = self.bindings.first(where: { patternBindingSyntax in
return patternBindingSyntax.initializer != nil
patternBindingSyntax.initializer != nil
}) else {
return false
}
Expand Down Expand Up @@ -326,7 +326,7 @@ private extension FunctionCallExprSyntax {

private extension SequenceExprSyntax {
/// Detect assignment expression:
/// varName[xxx] = ... // index
/// varName[xxx] = ... // index
/// varName = ...
func referencedVariable(for varName: String) -> ReferencedVariable? {
let exprList = self.elements
Expand All @@ -350,15 +350,15 @@ private extension SequenceExprSyntax {
position: exprList.positionAfterSkippingLeadingTrivia,
kind: .assignment(subscript: true)
)
} else if let declExpr = firstExpr.as(DeclReferenceExprSyntax.self),
}
if let declExpr = firstExpr.as(DeclReferenceExprSyntax.self),
declExpr.baseName.tokenKind == .identifier(varName) {
return ReferencedVariable(
name: varName,
position: exprList.positionAfterSkippingLeadingTrivia,
kind: .assignment(subscript: false)
)
} else {
return nil
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct ReduceIntoInsteadOfLoopRuleExamples {
}

extension ReduceIntoInsteadOfLoopRuleExamples {

private static let triggeringDictionaryExamples: [Example] = [
Example("""
var result: Dictionary<SomeType1, SomeType2> = [:]
Expand Down Expand Up @@ -118,4 +117,3 @@ extension ReduceIntoInsteadOfLoopRuleExamples {
"""),
]
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct ReduceIntoInsteadOfLoopRuleHelpers { }
extension VariableDeclSyntax {
/// Binding is a var: "`var` someVar = <...>"
var isVar: Bool {
return self.bindingSpecifier.tokenKind == .keyword(.var)
self.bindingSpecifier.tokenKind == .keyword(.var)
}

var identifier: String? {
Expand All @@ -20,7 +20,7 @@ extension VariableDeclSyntax {
/// `type`.
func firstPatternOf<T: PatternSyntaxProtocol>(_ type: T.Type) -> T? {
let result = self.bindings.first { patternBinding in
return patternBinding.pattern.as(type) != nil
patternBinding.pattern.as(type) != nil
}
return result?.pattern.as(type)
}
Expand Down