-
Notifications
You must be signed in to change notification settings - Fork 389
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
PSUseDeclaredVarsMoreThanAssignments: False positive when using ForEach-Object in special cases #934
Comments
Maybe same issue
|
Also here we have false negative:
Expected rule violation for the code outside of while loop but there was no message. |
Currently we collect all the ScriptBlocks on the list and then for each of them run the AnalyzeScriptBlockAst method. In the method firstly we add all the variable from assignments to the dictionary and then we check if they were used beside assignment instruction. It generates false positives when the nested ScriptBlock is analyzed separately from the parent ScriptBlock (ForEach loop example) and false negative when variable was assigned and used in nested ScriptBlock and then is assigned in the parent block (the example above). I think the rule should be rewritten so that it analyzes block instruction after instruction and stores two dictionaries: for the variables assigned in parent scopes and variables declared in current scope. When we encounter ScriptBlock the method should be called for it recursively. |
Related issue is #938. Your proposed changes are sound and would be an improvement. |
@bergmeister By the existing codebase you mean just the SSA changes? Probably not the whole ScriptAnalyzer codebase... ? Could you, please, point me out to some PRs with the SSA changes or code itself? |
@kalgiz Yes, by base I only meant the existing base around the SSA implementation. I am going to try to describe my understanding of SSA in this code base now to the best of my knowledge but take it with caution as I could be wrong since I did not have to implement/touch it until recently:
But coming back to the original question that we should aim to answer: |
@bergmeister Thanks a lot for thorough clarification. I will try to schedule some discussions with seniors more aware of the codebase and hopefully we can come with a decision soon. |
Another one which doesn't get picked up is Because something immediately assigned to the value in refPrefix it is assumed to have been used. It should be treated the same as
But in effect is treated as
|
Duplicate of #1163 |
Extracted from this comment here:
Steps to reproduce
Expected behavior
No
PSUseDeclaredVarsMoreThanAssignments
warningsActual behavior
1 or more
PSUseDeclaredVarsMoreThanAssignments
warnings in both casesEnvironment data
The text was updated successfully, but these errors were encountered: