PowerShell / PowerShell/PSScriptAnalyzer

PSPossibleIncorrectComparisonWithNull fails to work with typecasted variables

オープン
#943 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Area - Rules Issue - Enhancement
主要言語
C#
スター
2.2k
フォーク
414
平均マージ
13時間 1分
マージ済み PR(30日)
2

説明

Steps to reproduce

If you validate this script, it complains about PSPossibleIncorrectComparisonWithNull even though it is pretty clear $value must be a string.

Function Get-FooBar() {
    return "foobar"
}

[string]$value = (Get-FooBar)

if ($value -eq $null) {
    Write-Output 'value is $null'
} else {
    Write-Output 'value is not $null'
}

Expected behavior

It works perfectly fine if the value is type-casted like this:

Function Get-FooBar() {
    return "foobar"
}

$value = (Get-FooBar) -as [string]

if ($value -eq $null) {
    Write-Output 'value is $null'
} else {
    Write-Output 'value is not $null'
}

Actual behavior

So we get this error needlessly.

ERROR: test.ps1:9:5: PSPossibleIncorrectComparisonWithNull $null should be on the left side of equality comparisons.

Environment data

> $PSVersionTable
Name                           Value                                                                                               
----                           -----                                                                                               
PSVersion                      6.0.2                                                                                               
PSEdition                      Core                                                                                                
GitCommitId                    v6.0.2                                                                                              
OS                             Linux 4.15.10-1.el7.elrepo.x86_64 #1 SMP Thu Mar 15 10:05:52 EDT 2018                               
Platform                       Unix                                                                                                
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                             
PSRemotingProtocolVersion      2.3                                                                                                 
SerializationVersion           1.1.0.1                                                                                             
WSManStackVersion              3.0     

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.1

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

PSPossibleIncorrectComparisonWithNull ルールから始め、最初の PowerShell スクリプトを使って警告を再現し、その後 -as [string] の例と比較します。明示的に型変換された変数をルールがどのように処理するかを追跡し、示されている安全な比較では警告が発行されないように、カバレッジを追加または更新します。その一方で、既存のケースは引き続きカバーします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
powershell
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。