From adb508945a58b5978139d34d4abc2783fa7423a9 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Wed, 29 May 2019 09:39:42 +0100 Subject: [PATCH 1/3] Update PossibleIncorrectComparisonWithNull documentation with better example --- RuleDocumentation/PossibleIncorrectComparisonWithNull.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md index d0e186521..c5a3c53dd 100644 --- a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md +++ b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md @@ -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' } ``` From 4f26261f34f16aa3000beaeec26fc0e53a911167 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Thu, 30 May 2019 21:13:55 +0100 Subject: [PATCH 2/3] Update RuleDocumentation/PossibleIncorrectComparisonWithNull.md Co-Authored-By: Robert Holt --- RuleDocumentation/PossibleIncorrectComparisonWithNull.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md index c5a3c53dd..b6817b3e4 100644 --- a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md +++ b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md @@ -42,6 +42,6 @@ function Test-CompareWithNull ``` PowerShell # 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 (@() -eq $null) { 'true' } else { 'false' } if (@() -ne $null) { 'true' }else { 'false' } ``` From 1901be375e7d823c0ac623733f982fc69934f0f7 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Thu, 30 May 2019 21:19:45 +0100 Subject: [PATCH 3/3] Add example from Jim --- .../PossibleIncorrectComparisonWithNull.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md index b6817b3e4..bfcde4672 100644 --- a/RuleDocumentation/PossibleIncorrectComparisonWithNull.md +++ b/RuleDocumentation/PossibleIncorrectComparisonWithNull.md @@ -43,5 +43,16 @@ function Test-CompareWithNull ``` PowerShell # 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' } +if (@() -ne $null) { 'true' }else { 'false' } +``` +This is just the way how the comparison operator works (by design) but as demonstrated this can lead to unintuitive behaviour, especially when the intent is just a null check. The following example demonstrated the designed behaviour of the comparison operator, whereby for each element in the collection, the comparison with the right hand side is done, and where true, that element in the collection is returned. +``` PowerShell +PS> 1,2,3,1,2 -eq $null +PS> 1,2,3,1,2 -eq 1 +1 +1 +PS> (1,2,3,1,2 -eq $null).count +0 +PS> (1,2,$null,3,$null,1,2 -eq $null).count +2 ```