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 Errors on validateset #1970

Open
nascentt opened this issue Feb 28, 2024 · 4 comments · May be fixed by #1988
Open

PSAvoidAssignmentToAutomaticVariable Errors on validateset #1970

nascentt opened this issue Feb 28, 2024 · 4 comments · May be fixed by #1988

Comments

@nascentt
Copy link

I have been getting errors on validateset because PSScriptAnalyzer is confusing the validateset as assignment.

An example of this happening is

[validateset($True, $False)]
[bool]$UnfurlLinks,

[validateset($True, $False)]
[bool]$UnfurlMedia,

from https://github.com/RamblingCookieMonster/PSSlack/blob/master/PSSlack/Public/New-SlackMessage.ps1

@liamjpeters
Copy link
Contributor

This is a strange one.

Ultimately ValidateSet only accepts a string array of valid values, so I guess it's going to cast $True and $False to string and be left with 'True' and 'False'.

That's why, in the terminal, when I use the parameter completion that ValidateSet facilitates, I get the string options (not $true or $false):

image

But accepting the suggestions, the function errors:

New-SlackMessage: Cannot process argument transformation on parameter 'UnfurlLinks'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Which makes me think the function needs some work if it's using ValidateSet to ensure that a declared Boolean value is either true or false 🤔.

That all said, you're correct and it's not assignment. The rule is probably checking all parameters for any variable expressions and alerting if a reserved name is used.

I'll take a look at this.

@JamesWTruher
Copy link
Contributor

JamesWTruher commented Mar 27, 2024

This is a strange one.

Ultimately ValidateSet only accepts a string array of valid values, so I guess it's going to cast $True and $False to string and be left with 'True' and 'False'.

That's why, in the terminal, when I use the parameter completion that ValidateSet facilitates, I get the string options (not $true or $false):

image

But accepting the suggestions, the function errors:

New-SlackMessage: Cannot process argument transformation on parameter 'UnfurlLinks'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Which makes me think the function needs some work if it's using ValidateSet to ensure that a declared Boolean value is either true or false 🤔.

That all said, you're correct and it's not assignment. The rule is probably checking all parameters for any variable expressions and alerting if a reserved name is used.

I'll take a look at this.

it's not that $true and $false are being converted to a string. It's that they are constants from powershell's perspective, so they don't cause a parsing error. For example, if you use $HOME, you wouldn't get past the parsing stage since $HOME is not a constant from PowerShell's perspective. There are a very small set of variables which PowerShell considers constant.

This attribute is really looking for a set of constant strings

@liamjpeters
Copy link
Contributor

Thanks for the explanation @JamesWTruher. Makes sense!

@bergmeister
Copy link
Collaborator

Thanks for raising this and opening PR, will review

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