PowerShell / PowerShell/PSScriptAnalyzer
Rule request: AvoidUsingPlusEqualOnCollections
還沒有人認領這個 Issue。
- 主要語言
- C#
- 星號
- 2.2k
- 分支
- 415
- 平均合併
- 13 小時 1 分鐘
- 30 天內合併 PR
- 2
描述
Summary of the new feature
NOTE: I do believe this rule isn't relevant for v7.5+ but a lot of us are unfortunately stuck on v5.1.
As a code reviewer, I want developers to be warned about using += to build collections so that I don't have to repeatedly explain why their scripts have poor performance and can focus on reviewing logic instead of catching inefficient patterns.
Problem Statement:
Using += to build arrays and collections in PowerShell is a common performance anti-pattern. Each += operation creates an entirely new array and copies all existing elements, resulting in O(n²) complexity for building collections. This can cause significant performance degradation, especially with large datasets.
For example, adding 10,000 items to an array using += performs ~50 million copy operations, while using proper collection types performs only 10,000 add operations.
Proposed technical implementation details
Rule Name: PSAvoidUsingPlusEqualsOnCollections
Severity: Warning
Behavior:
- Skip if using PowerShell v7.5+
- Flag usage of
+=operator when the left-hand side is a collection, array, or string. - Suggest more efficient alternatives based on the context
Recommended alternatives to suggest:
- Microsoft's documentation has a good start to this:
Example violations:
# Building array with += - Flagged
$results = @()
foreach ($item in $data) {
$results += $item
}
# Building collection in loop - Flagged
$numbers = @()
for ($i = 1; $i -le 1000; $i++) {
$numbers += $i
}
# Adding to existing array - Flagged
$existingArray += $newItem
# Adding to IDictionaries
$hashtable = @{}
for ($i = 1; $i -le 1000; $i++)
{
$hashtable += @{$i = $i }
}
Should NOT be flagged:
# Numeric operations
$sum += $number
Technical Implementation:
- I plan on implementing this rule if approved.
- From my testing, the rule will inherit AstVisitor and visit
VisitAssignmentStatement. - Left-hand assignment's type can be retrieved via
Helper.Instance.GetTypeFromAnalysis() - If not found, analyze where the variable was initialized and go from there. From my testing, analyzing the right-hand side ExpressionAsts' helped determine said type if
GetTypeFromAnalysiswas unreliable. - Provide context-appropriate suggestions based on the use case
Configuration Options (up for discussion):
- Add type exclusion (e.g., String)?
What is the latest version of PSScriptAnalyzer at the point of writing
1.24.0
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先從提議的 AstVisitor 實作和 VisitAssignmentStatement 開始,接著檢查 Helper.Instance.GetTypeFromAnalysis() 以及 issue 中描述的初始化和型別分析路徑。在實作之前,定義 collections、arrays、strings、數值運算和 PowerShell 版本條件的處理方式。完成的標準是:該規則會針對指定的 collection += 情況回報問題並給出符合上下文的建議,同時不回報數值 +=。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- csharp, powershell
- 領域
- tooling
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100