Skip to content

PSAvoidUninitializedVariable flags automatic variables #192

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
RamblingCookieMonster opened this issue May 22, 2015 · 6 comments
Closed
Assignees

Comments

@RamblingCookieMonster
Copy link

Hi there!

My apologies if this has already been covered.

PSAvoidUninitializedVariable seems to flag automatic PowerShell variables, such as $Matches and $PSBoundParameters.

Example code:

"hi there!" -match "hi" | Out-Null
$matches[0]

Resulting warning:

Variable 'matches' is not initialized. Non-global variables must be initialized. To fix a violation of this rule, pl
ease initialize non-global variables.

Cheers!

@RamblingCookieMonster
Copy link
Author

Odd, getting an error trying to edit the issue. Was going to add that this could be added to @KirkMunro's issue #189.

Thanks!

@yutingc
Copy link
Contributor

yutingc commented May 22, 2015

Thanks @RamblingCookieMonster for reporting the issue. I think we've fixed the issue for $PSBoundParameter but I'm not sure about $Matches. Regarding #189, we will need to have a discussion about this rule within the ScriptAnalyzer team to evaluate its usefulness as well as the noise it generates.
We will get back to you soon! Thanks for trying out PSScriptAnalyzer :-)

@RamblingCookieMonster
Copy link
Author

Sounds good!

On a side note, spot checking other automatic variables from the following (incomplete) list, looks like it flags these as well.

[powershell]::Create().addscript({
    Get-Variable | Select -ExpandProperty Name
}).invoke()

Module version 1.0.1

Thanks!

Warren

@mwjcomputing
Copy link

I want to add that $PSVersionTable flags this warning as well.

@yutingc
Copy link
Contributor

yutingc commented May 26, 2015

Thanks everyone. We will take a look at this bug.

@quoctruong
Copy link

Fixed with #241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants