Skip to content

Commit 23095f6

Browse files
authored
Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a .Net property and only look at the LHS (#1008)
* Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a net property to a .Net property * when variableExpressionAst is null, then continue loop to reduce nesting * fix indentation for cleaner diff * Fix issue #1013 as well by making sure the rule looks only the LHS * Fix issue #1012 as well * Simplify test that checks that PSSA does not throw to address PR review
1 parent a22fd0f commit 23095f6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Rules/AvoidAssignmentToAutomaticVariable.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ 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 && testAst.Parent == assignmentStatementAst, searchNestedScriptBlocks: false) as VariableExpressionAst;
50+
if (variableExpressionAst == null) { continue; }
5051
var variableName = variableExpressionAst.VariablePath.UserPath;
5152
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase))
5253
{

Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ Describe "AvoidAssignmentToAutomaticVariables" {
6363
$warnings.Count | Should -Be 0
6464
}
6565

66+
It "Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" {
67+
Invoke-ScriptAnalyzer -ScriptDefinition '[foo]::bar = [baz]::qux' -ErrorAction Stop
68+
}
69+
70+
It "Does not flag properties of a readonly variable (issue 1012)" {
71+
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition '$Host.PrivateData["ErrorBackgroundColor"] = "Black"'
72+
$warnings.Count | Should -Be 0
73+
}
74+
75+
It "Does not flag RHS of variable assignment (Bug in 1.17.0, issue 1013)" {
76+
[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition '[foo]::bar = $true'
77+
$warnings.Count | Should -Be 0
78+
}
79+
6680
It "Setting Variable <VariableName> throws exception in applicable PowerShell version to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables {
6781
param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr)
6882

0 commit comments

Comments
 (0)