Skip to content

null-comparison-side

PowerShellcorrectnessno fixdefault: warnpowershell/psscriptanalyzer

Reports a -eq or -ne comparison (or a case variant, as -ceq) with $null on the right.

When the left side is an array, -eq and -ne filter it and return the elements: @(1, 2) -eq $null is an empty array and @(1, 2) -ne $null is the whole array. The test does not say whether the value itself is $null.

To fix, write $null on the left: $null -eq $x.

Reported
...
$count = 1
$x -eq $null
$null -eq $x
if ($a -ne $null -and $b -ceq $null) { }
$x -eq $nullable
$null = 1
$global:LASTEXITCODE = 1
...
powershell:
lint:
rules:
null-comparison-side: warn # off, warn (default), error

Enabled by the presets: powershell/psscriptanalyzer.

Suppress one line with sudsy-disable-next-line null-comparison-side in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
psscriptanalyzer PSPossibleIncorrectComparisonWithNull partial The module does not report a left operand it knows to be a scalar (a parameter or variable of a non-array type); Sudsy has no types, so [string]$a … $a -eq $null is reported. A comparison whose expression also holds -and, -or or -xor at the same level is skipped (the expression is not split). No fix that swaps the operands.