- 
                Notifications
    
You must be signed in to change notification settings  - Fork 403
 
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
          
     Merged
      
      
            JamesWTruher
  merged 5 commits into
  PowerShell:development
from
bergmeister:UseDeclaredVarsMoreThanAssignments_FixFalsePositives
  
      
      
   
  Mar 30, 2018 
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c9f97de
              
                fix false positives such as '$a=@(); $b | foreach-object { $a+=$c}' b…
              
              
                bergmeister 7daf5a4
              
                add test (it turns out a regression happened for 'flags strongly type…
              
              
                bergmeister bc1c007
              
                fix regression for strongly typed variables by being more selective a…
              
              
                bergmeister 077d035
              
                add additional test case for unassigned variable
              
              
                bergmeister 647efe7
              
                Merge branch 'development' of https://github.com/PowerShell/PSScriptA…
              
              
                bergmeister File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
what about the case where
$ais not initialized? Does that fall into this rule or a different one?Uh oh!
There was an error while loading. Please reload this page.
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.
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 usingSet-StrictModeusing version 1 or higher.I added your test case though because it is always good to have more test cases moving forward.