Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa7a582

Browse files
AvoidAssignmentToAutomaticVariable: Ignore when a Parameter has an Attribute that contains a Variable expression, such as '[ValidateSet($True,$False)]'. (#1988)
Co-authored-by: Christoph Bergmeister <c.bergmeister@gmail.com>
1 parent03beb17 commitaa7a582

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

‎Rules/AvoidAssignmentToAutomaticVariable.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8989
{
9090
continue;
9191
}
92-
92+
// also check the parent to exclude variableExpressions that appear within attributes,
93+
// such as '[ValidateSet($True,$False)]' where the read-only variables $true,$false appear.
94+
if(variableExpressionAst.ParentisAttributeAst)
95+
{
96+
continue;
97+
}
9398
if(_readOnlyAutomaticVariables.Contains(variableName,StringComparer.OrdinalIgnoreCase))
9499
{
95100
yieldreturnnewDiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError,variableName),

‎Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ Describe "AvoidAssignmentToAutomaticVariables" {
9494
$warnings.Count| Should-Be0
9595
}
9696

97+
It"Does not flag true or false being used in ValidateSet" {
98+
# All other read-only automatic variables cannot be used in ValidateSet
99+
# they result in a ParseError. $true and $false are permitted however.
100+
[System.Array]$warnings=Invoke-ScriptAnalyzer-ScriptDefinition'param([ValidateSet($true,$false)]$MyVar)$MyVar'-ExcludeRule PSReviewUnusedParameter
101+
$warnings.Count| Should-Be0
102+
}
103+
97104
It"Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" {
98105
Invoke-ScriptAnalyzer-ScriptDefinition'[foo]::bar = [baz]::qux'-ErrorAction Stop
99106
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp