FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

PSAvoidAssignmentToAutomaticVariable: Ignore when a Parameter has an Attribute that contains a Variable expression by liamjpeters · Pull Request #1988 · PowerShell/PSScriptAnalyzer · GitHub

PSAvoidAssignmentToAutomaticVariable: Ignore when a Parameter has an Attribute that contains a Variable expression - #1988

Merged
Andy Jordan (andyleejordan) merged 2 commits into
PowerShell:mainfrom
liamjpeters:#1970ErrorsOnValidateSet
Feb 19, 2025
Merged

PSAvoidAssignmentToAutomaticVariable: Ignore when a Parameter has an Attribute that contains a Variable expression#1988
Andy Jordan (andyleejordan) merged 2 commits into
PowerShell:mainfrom
liamjpeters:#1970ErrorsOnValidateSet

Conversation

Liam Peters (liamjpeters) commented Mar 27, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

PR Summary

The rule PSAvoidAssignmentToAutomaticVariable alerts when no assignment has taken place.

It does so when $true or $false is used within the ValidateSet attribute of a parameter. Only the automatic variables $true and $false are permitted here - all other read-only automatic variables are a ParseError.

Currently:

param(
  [ValidateSet($true, $false)]
  $MyVar
)

Is flagged by PSAvoidAssignmentToAutomaticVariable.

The Variable 'true' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.

Interestingly when a ValidateSet is used in this way outside of a param block - it does not get flagged.


This PR adds a check for variable expressions found within parameters which are the child of an Attribute, and skips them. Similarly to skipping over the variable expressions that are children of NamedAttributeArguments, which is already present.

This does not prevent the bodies of scriptblocks, within attributes, from being checked. So for instance the below is still flagged by PSAvoidAssignmentToAutomaticVariable.

Invoke-ScriptAnalyzer -ScriptDefinition 'param([ValidateScript({$true = $_})] $MyVar)' -ExcludeRule 'PSReviewUnusedParameter'

Resolves #1970

PR Checklist

…tribute that contains a Variable expression, such as '[ValidateSet($True,$False)]'.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks, looks good to me

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks!

Andy Jordan (andyleejordan) merged commit aa7a582 into PowerShell:main Feb 19, 2025
Liam Peters (liamjpeters) deleted the #1970ErrorsOnValidateSet branch February 21, 2025 11:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Successfully merging this pull request may close these issues.

PSAvoidAssignmentToAutomaticVariable Errors on validateset

3 participants


Back | FazBrowse Home | New Git URL