PowerShell / PowerShell/PSScriptAnalyzer

Rule request: AvoidUsingPlusEqualOnCollections

Đang mở
#2,112 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

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:

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 GetTypeFromAnalysis was 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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với phần triển khai AstVisitor và VisitAssignmentStatement được đề xuất, sau đó kiểm tra Helper.Instance.GetTypeFromAnalysis() và các đường dẫn khởi tạo/phân tích kiểu được mô tả trong issue. Xác định cách xử lý collections, arrays, strings, các phép toán số và điều kiện phiên bản PowerShell trước khi triển khai. Được xem là hoàn thành khi rule báo cáo các trường hợp collection += được chỉ định với các đề xuất phù hợp với ngữ cảnh, đồng thời không báo cáo += số.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, powershell
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.