PSAvoidAssignmentToAutomaticVariable generates false positives during script analysis
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 35/100
Línea de trabajo
Comienza ejecutando la función Write-Log proporcionada mediante PSScriptAnalyzer e inspeccionando la regla PSAvoidAssignmentToAutomaticVariable que informa sobre el parámetro $Error. Confirma el diagnóstico según el comportamiento de ámbito de PowerShell indicado; se considera completado cuando este caso no se informa y el texto del diagnóstico se corrige si sigue siendo aplicable en otro lugar.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Steps to reproduce
Run this function through PSSA.
function Write-Log {
[CmdletBinding()]
param
(
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Message,
[switch]
$Error
)
$callerEAP = $ErrorActionPreference
try {
$messageColor = if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Error') -and $Error) {
[ConsoleColor]::Red
} else {
[ConsoleColor]::Cyan
}
Write-Host -Foreground $messageColor -Object $Message
# Reset colors to return to default after an error message on a compilation error
[System.Console]::ResetColor()
} catch {
Write-Error -ErrorRecord $_ -ErrorAction $callerEAP
}
}
Expected behavior
It passes.
Actual behavior
PSSA reports the following:
The Variable 'Error' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.
There are a number of problems with this:
- The comma should be a period for it to read properly.
- The error text is incorrect.
$Errorcan be assigned in child scopes without issue.$Erroris read-only only in the global scope, and errors are automatically written into that globally-scoped collection that is read-only from PowerShell. In functions, or child scopes, you can use a$Errorvariable to your hearts content. - Most importantly: the issue that is being called out by PSSA here should not be called out at all.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.1
PSEdition Core
GitCommitId 7.0.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
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.19.0
- Lenguaje dominante
- C#
- Estrellas
- 2.2k
- Forks
- 415
- Merge medio
- 13 h 1 min
- PR fusionados (30 d)
- 2
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Dificultad 1/5 1-3 horas Aptitud para principiantes 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
PowerShell/PSScriptAnalyzer#2217 · 1 comentario ·
-
PSUseConsistentIndentation double-indents attribute bodies that open a scriptblock (`[Attr({ … })]`) Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 70/100
PowerShell/PSScriptAnalyzer#2216 · 2 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
PowerShell/PSScriptAnalyzer#2211 ·
-
`PSPlaceOpenBrace` and `PSPlaceCloseBrace` leave trailing whitespace when expanding one-line blocks Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 70/100
PowerShell/PSScriptAnalyzer#2210 ·
Todos los issues de PowerShell/PSScriptAnalyzer
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Dificultad 2/5 1-3 horas Aptitud para principiantes 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
SubtitleEdit/subtitleedit#15108 · 1 comentario ·
-
area/docs-content Bug pulumi/docs
Dificultad 1/5 1-3 horas Aptitud para principiantes 94/100
-
agentic-workflows untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100