-
Notifications
You must be signed in to change notification settings - Fork 394
Rule to warn against use of equal sign when comparator (-eq) should be used #809
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
Comments
I agree about But I have some doubts about $y = 0 # or $null, empty array, string
if ($x = $y) {'yes'} else {'no'} # it is 'no' I agree that Example: if ($x = <something>) {
... # work with $x
}
else {
throw "something is empty!"
} instead of the same $x = <something>
if ($x) {
... # work with $x
}
else {
throw "something is empty!"
} |
While I agree with the last example, it would be a very advanced concept for most scripters and more likely been done in error. Multiple times I have been tripped up by use of '=' in an 'if' statement, that reads syntactically correct for many other languages, but is an assignment statement in PowerShell. |
I agree with you, too. It's just going to be a noise rule for me... |
Can you allow the new rule "PSPossibleIncorrectUsageOfAssignmentOperator" to be suppressed by parenthesizing the condition? For example:
can be fixed in the script with
You can see this fix in other places like warn-assignment-condition.cpp
|
To be clear
|
@imfrancisd Interesting thought but I think this might spark a discussion from the PowerShell purists about unnecessary parenthesis. if ($a=$b){} # will warn because one should either use the variable or the assignment operator was used unintentionally
if ($a=$b){$c=$a} # will not warn because $a is referenced
if ($a=$b){$a=$c} # will not warn because $a is referenced
# similar behaviour applies to the 'else if' block |
Aversion to parentheses is a good heuristic!
If you see a purist use "unnecessary" parentheses like this
then you know they mean it, and it is not an accident! :-) Simple Rule, Simple Remedy, Simple EnforcementWith this approach (which is also used by C analyzers), the rule is simple:
Pitfalls to this approachThe complexity with this approach comes with conditions like this:
But in cases like this, I don't think anyone will complain about an analyzer telling them to change the code. |
Problem with checking if variable is usedHow do you know if this is okay?
The variable "$a" is used, but how do you know that "$a = $c" is what I intended? |
@imfrancisd I referenced your comments in PR 881 because that's where I'd like to see the discussion happen. You have interesting ideas so let's see what other people think about this new approach/mindset. About your last comment: It is hard and in some cases impossible to get 100% test case coverage but it is more important to limit the number of false positives as much as possible. This rule is brand new and therefore the approach is to first find cases where we know for sure that we should warn and once this works reasonably well, we can improve it further. |
When moving from other languages where a simple '=' equal sign can be used both as assignment and operator, a warning should be logged. Other Boolean operations should be flagged too. Examples:
if ($x = $y) {} -- Always true
for ($i = 1; $i > 99; $i++) - alias to out-file
The text was updated successfully, but these errors were encountered: