Unused variable false positive in `ForEach-Object` begin block (`PSUseDeclaredVarsMoreThanAssignments`)

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

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
58/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
powershell, typescript
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện chẩn đoán với ví dụ ForEach-Object được cung cấp trong tiện ích mở rộng PowerShell và kiểm tra cách các kết quả của PSUseDeclaredVarsMoreThanAssignments được xử lý. So sánh cảnh báo với các khối begin, process và end được hiển thị trong báo cáo. Hoàn tất có nghĩa là false positive không còn được báo cáo, trong khi các cảnh báo thực sự về biến không được sử dụng vẫn tiếp tục xuất hiện.

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

Mô tả

Area-Script Analysis Issue-Bug Pending: External
Prerequisites
  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.
Summary

Variables initialized in begin block of ForEach-Object trigger PSUseDeclaredVarsMoreThanAssignments warning.

PowerShell Version
Name                           Value
----                           -----
PSVersion                      5.1.26100.7705
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.26100.7705
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Name             : ConsoleHost
Version          : 5.1.26100.7705
InstanceId       : 4ed60023-e437-45ae-b589-226f399acc62
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
Visual Studio Code Version
1.109.5
072586267e68ece9a47aa43f8c108e0dcbf44622
x64
Extension Version
ms-vscode.powershell@2025.4.0
Steps to Reproduce
1, 2, 3 | ForEach-Object { $list = @() } { $list += $_ } { $list }
                           ^^^^^
The variable 'list' is assigned but never used.
PSScriptAnalyzer(PSUseDeclaredVarsMoreThanAssignments)

Returning the $list from the begin block offs the warning

1, 2, 3 | ForEach-Object { $list = @(); $list } { $list += $_ } { $list }
Visuals

No response

Logs

No response

Ngôn ngữ chính
TypeScript
Star
1.9k
Fork
548
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

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.

Issue khác của PowerShell/vscode-powershell

Tất cả issue của PowerShell/vscode-powershell

Issue tương tự

Thêm issue về TypeScript

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.