PSAvoidAssignmentToAutomaticVariable: Ignore when a Parameter has an Attribute that contains a Variable expression
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
ValidateSetis 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
- [x] PR has a meaningful title
- Use the present tense and imperative mood when describing your changes
- [x] Summarized changes
- [x] Change is not breaking
- [x] Make sure all
.cs,.ps1and.psm1files have the correct copyright header - [x] Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
- [x] This PR is ready to merge and is not Work in Progress.
- If the PR is work in progress, please add the prefix
WIP:to the beginning of the title and remove the prefix when the PR is ready.
- If the PR is work in progress, please add the prefix