Skip to content

PSUseDeclaredVarsMoreThanAssignments is thrown incorrectly #903

Closed
@essentialexch

Description

@essentialexch

Steps to reproduce

$files = Get-ChildItem -Recurse $folder
$str = $files.Count.ToString( 'n0' )
$totalSize = 0
$files |
 Where-Object   { $_.PSIsContainer -eq $false } | 
 ForEach-Object { $totalSize += $_.Length } # warning for $totalSize
$str = $totalSize.ToString( 'n0' )

Expected behavior

On line 7, nothing should be reported.

Actual behavior

On line 7, $totalSize is underlined with a green squiggle, "The variable 'totalSize' is assigned but never used. (PSUseDeclaredVarsMoreThanAssignments)"; which is obviously false.


Environment data
----------------

Latest VSCode on Windows. PS 5.1 with Win10 patched to current.

<!-- Provide the output of the following 2 commands -->

```powershell
> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.98
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.98
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

PS C:\Users\michael> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.1
PS C:\Users\michael>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions