-
Notifications
You must be signed in to change notification settings - Fork 393
Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a .Net property and only look at the LHS #1008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a .Net property and only look at the LHS #1008
Conversation
… when assigning a net property to a .Net property
@@ -47,18 +47,21 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | |||
foreach (var assignmentStatementAst in assignmentStatementAsts) | |||
{ | |||
var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; | |||
var variableName = variableExpressionAst.VariablePath.UserPath; | |||
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) | |||
if (variableExpressionAst != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only this null check was added, all the code diff below is only the added indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from a flow perspective, this could also be
if ( variableExpressionAst == null )
continue
and the indenting is preserved :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neither of my comments are blockers
@@ -63,6 +63,21 @@ Describe "AvoidAssignmentToAutomaticVariables" { | |||
$warnings.Count | Should -Be 0 | |||
} | |||
|
|||
It "Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" { | |||
$exceptionThrown = $false | |||
try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this just be:
{ Invoke-ScriptAnalyzer -ScriptDefinition '[foo]::bar = [baz]::quz' -ErrorAction Stop } | Should Not Throw
It will make the log easier to understand if it fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pester only works with terminating errors. PSSA throws non-terminating errors, see pester/Pester#366
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would have thought that -ErrorAction Stop
would do the trick (after all, it's in a try/catch, so you're doing the same thing) so somebody is halting the pipeline
@@ -47,18 +47,21 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | |||
foreach (var assignmentStatementAst in assignmentStatementAsts) | |||
{ | |||
var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; | |||
var variableName = variableExpressionAst.VariablePath.UserPath; | |||
if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) | |||
if (variableExpressionAst != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from a flow perspective, this could also be
if ( variableExpressionAst == null )
continue
and the indenting is preserved :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@@ -63,6 +63,21 @@ Describe "AvoidAssignmentToAutomaticVariables" { | |||
$warnings.Count | Should -Be 0 | |||
} | |||
|
|||
It "Does not throw a NullReferenceException when using assigning a .Net property to a .Net property (Bug in 1.17.0 - issue 1007)" { | |||
$exceptionThrown = $false | |||
try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would have thought that -ErrorAction Stop
would do the trick (after all, it's in a try/catch, so you're doing the same thing) so somebody is halting the pipeline
PR Summary
variableExpressionAst
The issues are slightly related to each other, therefore they are both addressed in this PR to keep things simple.
PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
x
between the square brackets. Please mark anything not applicable to this PRNA
.WIP:
to the beginning of the title and remove the prefix when the PR is ready