PowerShell / PowerShell/vscode-powershell
Unused variable false positive in `ForEach-Object` begin block (`PSUseDeclaredVarsMoreThanAssignments`)
Nessuno ha ancora preso questa issue.
- Lingua principale
- TypeScript
- Stelle
- 1.9k
- Fork
- 548
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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
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.
Direzione di ricerca
Inizia riproducendo la diagnostica con l’esempio ForEach-Object fornito nell’estensione PowerShell e verifica come vengono gestiti i risultati di PSUseDeclaredVarsMoreThanAssignments. Confronta l’avviso con i blocchi begin, process ed end mostrati nel report. Il lavoro è completato quando il falso positivo non viene più segnalato, mentre gli avvisi autentici sulle variabili inutilizzate continuano a comparire.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- powershell, typescript
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 58/100