-
Notifications
You must be signed in to change notification settings - Fork 394
Warn when an if statement contains an assignment #859
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
Warn when an if statement contains an assignment #859
Conversation
…se where the execution block of the if statement contains an assignment
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 don't think this is quite ready yet. There are a couple of problems which should be addressed first. There is a pattern which is very useful which will cause warnings to appear:
if ( $files = get-childitem ) {
... do something ...
}
else {
... do something else ...
}
I think if you can be more specific about the test of 2 variables in the ifstatementast
(which should help you address the issue you found in the assignment outside of the test.
Hmm, such a case was discussed in the issue as well but my thinking was that the helpfulness of the warning makes up for those cases where the warning can be ignored. But you have a fair point, I will have a think if it is possible to get more certainty about the use case of the assignment operator. |
The
when run, you can see what happens!
|
…e sure that the following case does not get flagged up (added a test case for it) 'if ( $files = get-childitem ) { }'
Thanks for the pointers. I improved it but still need to make sure that the case which you pointed out does not get flagged up (I added a test for it in a TDD manner). I will let you know once the PR is ready for re-review. |
…nly false positive that is left is 'if ($a = (Get-ChildItem)){}' where the RHS is wrapped in an exprssion
@JamesWTruher : The PR is ready for review now. I have improved it to not flag false positives like |
{ | ||
// Check if someone used '==', which can easily happen when the person is used to coding a lot in C#. | ||
// In most cases, this will be a runtime error because PowerShell will look for a cmdlet name starting with '=', which is technically possible to define | ||
if (assignmentStatementAst.Right.Extent.Text.StartsWith("=")) |
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 will miss the case of
$a = = $b
but that's probably ok
Closes #809
This is a simple prototype that checks if there is an assignment ast inside an if statement.
It currently works and catches cases like
if( ($a = $b_){}
andif( ($a = $b_){}
but also warns if the execution block of the if statement contains an assignment like e.g. inif ($a -eq $b){$a=$b}
. This needs to be fixed and is being tracked by the currently failing test.The idea is to keep it simple to avoid false positives. For example the rule does not catch cases where there is an evalution inside the if statemenet as e.g. in
if ( ($a == $b) ){}