Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upWarn when an if statement contains an assignment #859
Conversation
|
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:
I think if you can be more specific about the test of 2 variables in the |
|
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. |
|
@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("=")) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

bergmeister commentedJan 28, 2018
•
edited
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) ){}