PowerShell / PowerShell/PSScriptAnalyzer
PSPossibleIncorrectComparisonWithNull fails to work with typecasted variables
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- C#
- Estrelas
- 2.2k
- Forks
- 415
- Merge médio
- 13h 1min
- PRs com merge (30d)
- 2
Descrição
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
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece pela regra PSPossibleIncorrectComparisonWithNull e reproduza o aviso usando o primeiro script do PowerShell; em seguida, compare-o com o exemplo -as [string]. Rastreie como a regra trata variáveis convertidas explicitamente para um tipo e adicione ou atualize a cobertura para que o aviso não seja emitido para a comparação segura demonstrada, mantendo os casos existentes cobertos.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- powershell
- Domínio
- tooling
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 48/100