PowerShell / PowerShell/PSScriptAnalyzer

PSPossibleIncorrectComparisonWithNull fails to work with typecasted variables

Đang mở
#943 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Area - Rules Issue - Enhancement
Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với quy tắc PSPossibleIncorrectComparisonWithNull và tái hiện cảnh báo bằng script PowerShell đầu tiên, sau đó so sánh với ví dụ -as [string]. Theo dõi cách quy tắc xử lý các biến được ép kiểu rõ ràng và thêm hoặc cập nhật độ bao phủ để cảnh báo không được phát ra đối với phép so sánh an toàn được minh họa, đồng thời vẫn bao phủ các trường hợp hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
powershell
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.