Skip to content

Commit 012b25c

Browse files
authored
Replaced duplicate link and improved examples for $null (#273)
* Replaced duplicate link * Fixes AB#304282
1 parent ff80de6 commit 012b25c

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

reference/docs-conceptual/PSScriptAnalyzer/Rules/PossibleIncorrectComparisonWithNull.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Null Comparison
3-
ms.date: 06/28/2023
3+
ms.date: 12/03/2024
44
ms.topic: reference
55
title: PossibleIncorrectComparisonWithNull
66
---
@@ -18,8 +18,8 @@ There are multiple reasons why this occurs:
1818
- `$null` is a scalar value. When the value on the left side of an operator is a scalar, comparison
1919
operators return a **Boolean** value. When the value is a collection, the comparison operators
2020
return any matching values or an empty array if there are no matches in the collection.
21-
- PowerShell performs type casting left to right, resulting in incorrect comparisons when `$null` is
22-
cast to other scalar types.
21+
- PowerShell performs type casting on the right-hand operand, resulting in incorrect comparisons
22+
when `$null` is cast to other scalar types.
2323

2424
The only way to reliably check if a value is `$null` is to place `$null` on the left side of the
2525
operator so that a scalar comparison is performed.
@@ -55,10 +55,10 @@ function Test-CompareWithNull
5555
## Try it Yourself
5656

5757
```powershell
58-
# Both expressions below return 'false' because the comparison does not return an
59-
# object and therefore the if statement always falls through:
58+
# This example returns 'false' because the comparison does not return any objects from the array
6059
if (@() -eq $null) { 'true' } else { 'false' }
61-
if (@() -ne $null) { 'true' } else { 'false' }
60+
# This example returns 'true' because the array is empty
61+
if ($null -ne @()) { 'true' } else { 'false' }
6262
```
6363

6464
This is how the comparison operator works by-design. But, as demonstrated, this can lead

reference/docs-conceptual/PSScriptAnalyzer/rules-recommendations.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: This article lists best-practice recommendations and the rules associated with them.
3-
ms.date: 06/28/2023
3+
ms.date: 12/03/2024
44
title: PSScriptAnalyzer rules and recommendations
55
---
66
# PSScriptAnalyzer rules and recommendations
@@ -23,7 +23,7 @@ No rules defined.
2323
- Cmdlets names with unusable characters [AvoidReservedCharInCmdlet][30]
2424
- Parameter names that can't be used [AvoidReservedParams][31]
2525
- Support confirmation requests [UseShouldProcessForStateChangingFunctions][37] and
26-
[UseShouldProcessForStateChangingFunctions][37]
26+
[UseSupportsShouldProcess][39]
2727
- Must call **ShouldProcess** when the **ShouldProcess** attribute is present and vice versa
2828
[UseShouldProcess][32]
2929
- Nouns should be singular [UseSingularNouns][38]
@@ -222,6 +222,7 @@ No rules defined.
222222
[36]: Rules/UsePSCredentialType.md
223223
[37]: Rules/UseShouldProcessForStateChangingFunctions.md
224224
[38]: Rules/UseSingularNouns.md
225+
[39]: Rules/UseSupportsShouldProcess.md
225226
[116]: https://github.com/PowerShell/PSScriptAnalyzer/issues/116
226227
[130]: https://github.com/PowerShell/PSScriptAnalyzer/issues/130
227228
[131]: https://github.com/PowerShell/PSScriptAnalyzer/issues/131

0 commit comments

Comments
 (0)