-
Notifications
You must be signed in to change notification settings - Fork 395
Update PossibleIncorrectComparisonWithNull documentation with better example #1244
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ function Test-CompareWithNull | |
## Try it Yourself | ||
|
||
``` PowerShell | ||
if (@() -eq $null) { 'true' } else { 'false' } # Returns false | ||
if ($null -ne @()) { 'true' } else { 'false' } # Returns true | ||
# Both expressions below return 'false' because the comparison does not return an object and therefore the if statement always falls through: | ||
if (@() -eq $null) { 'true' }else { 'false' } | ||
if (@() -ne $null) { 'true' }else { 'false' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this just a repetition of the line above? I think the existing examples make sense and are illustrative of the difference between a naive comparison with It might possibly be worth a more complicated example, but the purpose of the documentation here really isn't to teach people PowerShell concepts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of changing the example was that the existing example was too confusing for people because it changed 2 things at once, the example in this PR changes only 1 thing (eq to ne) to demonstrate the behaviour that might be unexpected for some people (yes, I know this is how the comparison operator works but most people do not use it with this intention and rather just want a null checks) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two points there:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first, yes it looks like it is the same example twice (if one already understands the root cause) but the point of the example is to also show on a high level (if the LHS and RHS were a complex, unknown expression) that changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I honestly missed the operator change. Without trying to blame my own failing on an extrinsic factor, I suspect others might also miss the important difference. Maybe it's worth breaking up the comment so there's one for each example, like: # In PowerShell, `$array -operator $value` works the same as `$array | Where-Object -operator $value` because of pipeline enumeration
# For example:
@(1, 2, 3, 4) -ge 3 # Emits `3, 4`
'a','b','c','ab' -like 'a*' # Emits 'a','ab'
'hi',$null,'bye' -ne $null # Emits 'hi','bye'
@() -ne $null` # Emits nothing (an empty array)
# Equality comparison doesn't work as often intended, because `@() -ne $null` results in a falsey empty value
if (@() -ne $null) { 'true' } else { 'false' } # Emits 'false'
# Comparison misleadingly appears to work as intended with -eq, but again because a falsey empty value is emitted (not because the `@()` value is non-null)
if (@() -eq $null) { 'true' } else { 'false' } # Emits 'false'
# Because of this caveat, PSScriptAnalyzer warns about comparisons with $null by default
# If you want to use a filter behaviour, it's recommended to explicitly filter instead:
# Don't use
if ($array -ne $null) { ... }
# DO use
if ($array | Where-Object -ne $null) { ... } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yee, I agree, maybe it is best to have an additional section with more examples so people can go deep and look at different scenarios and root causes, please open a PR for it since this PR got already merged. It makes me wonder if PowerShell would benefit e.g. from a |
||
``` |
Uh oh!
There was an error while loading. Please reload this page.