Skip to content

PsScriptAnalyzer Falsely Says Variable Is Assigned But Not Used but it actually is #1160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stumper66 opened this issue Jan 10, 2018 · 2 comments

Comments

@stumper66
Copy link

System Details

  • VSCode Version: Code 1.19.1 (0759f77bb8d86658bc935a10a64f6182c5a1eeba, 2017-12-19T09:46:23.884Z)
  • OS Version: Windows_NT x64 10.0.15063
  • PowerShell extension version: 1.5.1
  • Output from $PSVersionTable:
    PSVersion 5.1.15063.786
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
    BuildVersion 10.0.15063.786
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1
Copy / paste the following commands into the PowerShell Integrated Console, and paste the output here:

code -v
1.19.1
0759f77bb8d86658bc935a10a64f6182c5a1eeba
x64
$pseditor.EditorServicesVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
1      5      1      0
code --list-extensions --show-versions
[email protected]
[email protected]
[email protected]
[email protected]
$PSVersionTable
PSVersion                      5.1.15063.786
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.786
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

For some reason it always complains that my variable is assigned but never used when I assign into a particular loop. Repro code:

$RequredFiles = @("c:\somefile1.txt","c:\somefiles2.txt")
$IsMissingFiles = $false

$RequredFiles | ForEach-Object {
if (!(Test-Path $)){
$IsMissingFiles = $true
Write-Host "Required file missing: $
"
}
}

if ($IsMissingFiles){
Write-Host "Missing files"
}

Shows error:
file: 'file:///c%3A//test.ps1'
severity: 'Warning'
message: 'The variable 'IsMissingFiles' is assigned but never used. (PSUseDeclaredVarsMoreThanAssignments)'
at: '6,9'
source: 'PSScriptAnalyzer'
code: 'PSScriptAnalyzerdb99606b-bfb5-43be-8902-10233d7a4a8c'
I am experiencing a problem with...
False error message

Attached Logs

Follow the instructions in the README
about capturing and sending logs.
ps error

@klmj
Copy link

klmj commented Feb 6, 2018

As explained in Powershell Code format (PSScriptAnalyzer) flags variable as unused #889, this has aleady been reported here : PowerShell/PSScriptAnalyzer#827

Go there and +1 this issue 😃

@rjmholt
Copy link
Contributor

rjmholt commented Sep 20, 2018

It looks like PSSA has fixed this now. Going to close

@rjmholt rjmholt closed this as completed Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants