PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.8k stars 365 forks source link

$null should be on the left side of equality #1906

Closed 0x7FFFFFFFFFFFFFFF closed 1 year ago

0x7FFFFFFFFFFFFFFF commented 1 year ago

Summary of the new feature

I have this code

if ($var1 -eq $null) {
    $var1 = $var2
}

And the analyzer tells me:

$null should be on the left side of equality comparisons.PSScriptAnalyzer(PSPossibleIncorrectComparisonWithNull)

In fact, the tradition to put the value on the left side of the comparison comes from the confusion between the assignment operator and the equality operator, = and == in C-like languages. In PowerShell, the assignment operator is =, and the equality operator is -eq. So it's impossible to confuse them. And put the variable on the left side of the comparison is more readable and natural. Should we remove this rule from the analyzer?

StevenBucher98 commented 1 year ago

We don't plan to remove this rule by default, but you can suppress this rule by adding -ExcludeRule PSPossibleIncorrectComparisonWithNull to you Invoke-ScriptAnalyzer cmdlet.

0x7FFFFFFFFFFFFFFF commented 1 year ago

Thanks for the clarification.