Skip to content
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

PSAvoidAssignmentToAutomaticVariable Should detect auto-vars as function parameters #1026

Open
ghost opened this issue Jun 15, 2018 · 5 comments

Comments

@ghost
Copy link

ghost commented Jun 15, 2018

I did this recently and needed about 30 minutes to find my bug:

function Demo
{
    Param (
    [string] $input
    )
}

You can pass everything to this parameter, no warning, no error and $input is always $null !

See here, too: #712 "New Rule Suggestion: AvoidReuseOfAutomaticVariables"

@bergmeister
Copy link
Collaborator

@madmidi in PSScriptAnalyzer 1.17.1 there is already a new rule PSAvoidAssignmentToAutomaticVariable that catches assignments against the automatic variables listed here
It was decided to first start only with the read-only automatic variables because they would always result in a runtime error. Others need to be carefully considered since sometimes assignment can be by design for some of the automatic variables. I am relatively sure that we could add $input to this list though. :-)
Thanks for the feedback

@ghost
Copy link
Author

ghost commented Jun 18, 2018

pls have a look to the issue title.
it's not about $input, but about auto vars as function parameters !

@bergmeister
Copy link
Collaborator

@madmidi A function parameter with a read-only automatic variable such as $error does warn, as I said, the $input variable is just not yet part of the list of variables to warn against:

function Demo
{
    Param (
    [string] $error
    )
}
Invoke-ScriptAnalyzer C:\Users\cberg\Desktop\a.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidAssignmentToAutomaticVariabl Error        a.ps1      4     The Variable 'error' cannot be assigned since it is a
e                                                                 readonly automatic variable that is built into PowerShell,
                                                                  please use a different name.

@sicklittlemonkey
Copy link

I did the exact same thing last night. Half an hour of my life wasted, but I suppose at least I know about $input now.

The names for these automatic variables are truly stupid. They should have a differentiator in naming or syntax.

@fflaten
Copy link
Contributor

fflaten commented Feb 27, 2023

Should be fixed by 5d529a3

image

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

3 participants