Skip to content

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

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Jun 2, 2018

PR Summary

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 PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • 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

… when assigning a net property to a .Net property
@bergmeister bergmeister added this to the 1.17.1 milestone Jun 2, 2018
@bergmeister bergmeister self-assigned this Jun 2, 2018
@bergmeister bergmeister requested a review from JamesWTruher June 2, 2018 20:53
@@ -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)
Copy link
Collaborator Author

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

Copy link
Contributor

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 :)

Copy link
Contributor

@JamesWTruher JamesWTruher left a 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
Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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)
Copy link
Contributor

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 :)

@bergmeister bergmeister changed the title Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a net property to a .Net property Fix NullReferenceException in AvoidAssignmentToAutomaticVariable rule when assigning a .Net property and only look at the LHS Jun 4, 2018
Copy link
Contributor

@JamesWTruher JamesWTruher left a 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
Copy link
Contributor

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

@bergmeister bergmeister merged commit 23095f6 into PowerShell:development Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment