Skip to content

Fix PSUseDeclaredVarsMoreThanAssignments to not give false positives when using += operator #935

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 Mar 14, 2018

PR Summary

Hooray, this is the fix for the most common false positive of PSSA.
The main issue was that it was treating += as assignment. There are more special cases that need to be looked at like e.g. if the scriptblock is in the context of dot-sourcing but this is a different story, therefore getting the false positives fixed first, will, make the overall experience in most cases much better since some people are excluding the rule due to the many false positives.

Fixes #699
Fixes #827
Fixes #903 # Note to myself that other bugs in the comment of this issue should be extracted
Fixes PowerShell/vscode-powershell#776

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

@@ -73,5 +73,10 @@ function MyFunc2() {
It "returns no violations" {
$noViolations.Count | Should -Be 0
}

It "Does not flag += operator" {
$results = Invoke-ScriptAnalyzer -ScriptDefinition '$array=@(); $list | ForEach-Object { $array += $c }' | Where-Object { $_.RuleName -eq $violationName }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the case where $a is not initialized? Does that fall into this rule or a different one?

Copy link
Collaborator Author

@bergmeister bergmeister Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already covered because the way the algorithm works is that it first adds variable assignments to a list and then removes variables from this list when they are being used and at the end the left-over variables are basically the unused variables. In this case, += was not seen as a usage and therefore not being removed from this list. As a result, not initialising the variable does not affect the analysis because it already only looks for variable usages that are not in the list of assigned variables.
Using the += operator on an uninitialised variable might seem to be dangerous at first but it makes no problem in PowerShell at all therefore I do not think we should warn when doing that. It would only be a problem when using Set-StrictMode using version 1 or higher.
I added your test case though because it is always good to have more test cases moving forward.

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.

generally this looks fine, but I've got a question in the tests

@JamesWTruher JamesWTruher merged commit 6a8e828 into PowerShell:development Mar 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment