Rule request: AvoidUsingPlusEqualOnCollections
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia con l’implementazione proposta di AstVisitor e VisitAssignmentStatement, quindi esamina Helper.Instance.GetTypeFromAnalysis() e i percorsi di inizializzazione e analisi dei tipi descritti nell’issue. Definisci la gestione di collections, arrays, strings, operazioni numeriche e della condizione sulla versione di PowerShell prima dell’implementazione. Il lavoro è completato quando la regola segnala i casi specificati di collection += con suggerimenti appropriati al contesto, lasciando non segnalato il += numerico.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- C#
- Stelle
- 2.2k
- Fork
- 415
- Merge medio
- 13h 1m
- PR unite (30g)
- 2
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Difficoltà 1/5 1-3 ore Idoneità per principianti 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
PowerShell/PSScriptAnalyzer#2217 · 1 commento ·
-
PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 70/100
PowerShell/PSScriptAnalyzer#2216 · 2 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
PowerShell/PSScriptAnalyzer#2211 ·
-
`PSPlaceOpenBrace` and `PSPlaceCloseBrace` leave trailing whitespace when expanding one-line blocks Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 70/100
PowerShell/PSScriptAnalyzer#2210 ·