PowerShell / PowerShell/PSScriptAnalyzer
PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions
還沒有人認領這個 Issue。
- 主要語言
- C#
- 星號
- 2.2k
- 分支
- 414
- 平均合併
- 13 小時 1 分鐘
- 30 天內合併 PR
- 2
描述
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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先重現提供的 InitVars 和 UseVars 範例,接著追蹤 PSUseDeclaredVarsMoreThanAssignment 規則及其現有測試。當函式內的全域指派在之後被參照時能被辨識為已使用,同時目前的指令碼範圍行為維持不變,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- csharp, powershell
- 領域
- tooling
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100