Skip to content

Commit 8136638

Browse files
committed
Fix issue PowerShell#1013 as well by making sure the rule looks only the LHS
1 parent e4d1c72 commit 8136638

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Rules/AvoidAssignmentToAutomaticVariable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4444
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
4545

4646
IEnumerable<Ast> assignmentStatementAsts = ast.FindAll(testAst => testAst is AssignmentStatementAst, searchNestedScriptBlocks: true);
47-
foreach (var assignmentStatementAst in assignmentStatementAsts)
47+
foreach (AssignmentStatementAst assignmentStatementAst in assignmentStatementAsts)
4848
{
49-
var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst;
49+
var variableExpressionAst = assignmentStatementAst.Left.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst;
5050
if (variableExpressionAst == null) { continue; }
5151
var variableName = variableExpressionAst.VariablePath.UserPath;
5252
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))

Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Describe "AvoidAssignmentToAutomaticVariables" {
7777
$exceptionThrown | Should -Be $false
7878
}
7979

80+
It "Does not flag RHS of variable assignment (Bug in 1.17.0, issue 1013)" {
81+
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition '[foo]::bar = $true'
82+
$warnings.Count | Should -Be 0
83+
}
8084

8185
It "Setting Variable <VariableName> throws exception in applicable PowerShell version to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables {
8286
param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr)

0 commit comments

Comments
 (0)