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
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
Support array initialisation expression
  • Loading branch information
snofla committed May 12, 2025
commit 7ff5b30968513cd85f0cc7805fa7b0ea77b5e895
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private extension InitializerClauseSyntax {
/// Otherwise checks for `FunctionCallExpr`,`MemberAccessExpr`,`DictionaryExpr` and `ArrayExpr`:
/// 1. `= Set<T>(...)` | `Set(...)` | `.init(...)` | `[]`
/// 2. `= Array<T>(...)` | `Array(...)` | `.init(...)` | `[]`
/// 2b. `= [Type]()`
/// 3. `= Dictionary<K, V>()` | `Dictionary()` | `.init(..)` | `[:]`
func isTypeInitializer(for collectionType: CollectionType? = nil) -> Bool {
func isSupportedType(with name: String) -> Bool {
Expand All @@ -238,6 +239,15 @@ private extension InitializerClauseSyntax {
}
if let memberAccessExpr = functionCallExpr.calledExpression.as(MemberAccessExprSyntax.self),
memberAccessExpr.declName.baseName.tokenKind == .keyword(.`init`) {
// found a collection initialisation expression of `.init()`
// e.g. var array: [Int] = .init()
return true
}
if let arrayExpr = functionCallExpr.calledExpression.as(ArrayExprSyntax.self),
arrayExpr.elements.count == 1,
arrayExpr.elements.first?.expression.is(DeclReferenceExprSyntax.self) == true {
// found an array initialisation expression of `[type]`()
// e.g. var array = [Int]()
return true
}
} else if collectionType == .dictionary,
Expand Down