PowerShell / PowerShell/PSScriptAnalyzer
PSPossibleIncorrectComparisonWithNull fails to work with typecasted variables
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 2.2k
- Fork
- 414
- Merge medio
- 13h 1m
- PR unite (30g)
- 2
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la regola PSPossibleIncorrectComparisonWithNull e riproduci l'avviso usando il primo script PowerShell, quindi confrontalo con l'esempio -as [string]. Traccia la gestione da parte della regola delle variabili convertite esplicitamente a un tipo e aggiungi o aggiorna la copertura in modo che l'avviso non venga emesso per il confronto sicuro mostrato, mantenendo al contempo coperti i casi esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- powershell
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 48/100