Rule request: AvoidUsingPlusEqualOnCollections
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Beginne mit der vorgeschlagenen AstVisitor-Implementierung und VisitAssignmentStatement. Untersuche anschließend Helper.Instance.GetTypeFromAnalysis() sowie die im Issue beschriebenen Initialisierungs- und Typanalysepfade. Lege vor der Implementierung fest, wie Collections, Arrays, Strings, numerische Operationen und die PowerShell-Versionsbedingung behandelt werden. Als abgeschlossen gilt die Aufgabe, wenn die Regel die angegebenen Collection-+=-Fälle mit kontextgerechten Vorschlägen meldet, während numerisches += nicht gemeldet wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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
- Vorherrschende Sprache
- C#
- Sterne
- 2.2k
- Forks
- 422
- Ø Merge
- 2 Std. 23 Min.
- Gemergte PRs (30 T.)
- 25
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 1-2 Tage Anfängerfreundlichkeit 18/100
PowerShell/PSScriptAnalyzer#2302 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 55/100
PowerShell/PSScriptAnalyzer#2300 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember)Evtl. vergeben @jessehouwing hat das vor 89 Tagen übernommen. OffenIssue - Bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 52/100
PowerShell/PSScriptAnalyzer#2229 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 32/100
PowerShell/PSScriptAnalyzer#2222 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in PowerShell/PSScriptAnalyzer
Ähnliche Issues
-
kind/bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
unoplatform/uno#26555 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Listenarrs/Listenarr#1173 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 77/100
CactuseSecurity/firewall-orchestrator#5739 ·
Maintainer antworten meist innerhalb von 1 Tag
-
chore project-system small
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
vikramlearning/blazorbootstrap#1388 ·
Maintainer antworten meist innerhalb von 1 Tag