PowerShell / PowerShell/PSScriptAnalyzer
PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- C#
- Sterne
- 2.2k
- Forks
- 414
- Ø Merge
- 13 Std. 1 Min.
- Gemergte PRs (30 T.)
- 2
Beschreibung
This rule does not correctly take into account global variables if they are assigned to within a function.
If I set a global variable and then use it later, I still see a warning that the global variable was assigned to, but never used.
Given the following example:
function InitVars()
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
param()
$global:globalFoo = $false
$script:scriptFoo = $false
}
function UseVars()
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
param()
if ($global:globalFoo)
{
Write-Information "`$global:globalFoo is true"
}
if ($scriptFoo)
{
Write-Information "`$script:scriptFoo is true"
}
}
I get the following warning:
- The variable 'globalFoo' is assigned but never used. (line 6)
There are scenarios where using global variables is still necessary, and so in those few instances I am suppressing PSAvoidGlobalVars. To do that, I have to do the assignment within a method.
As you can see above, it hits with global scoped variables, but not with script scoped variables.
This issue doesn't happen if I leave the global assignment in the root of the file (as opposed to within InitVars, but then I can't use SuppressMessageAttribute.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das bereitgestellte InitVars- und UseVars-Beispiel zu reproduzieren, und verfolge dann die Regel PSUseDeclaredVarsMoreThanAssignment und ihre bestehenden Tests. Als abgeschlossen gilt die Aufgabe, wenn die globale Zuweisung innerhalb einer Funktion als verwendet erkannt wird, sobald später auf sie verwiesen wird, während das aktuelle Verhalten für den Skriptbereich unverändert bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- csharp, powershell
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100