PowerShell / PowerShell/PSScriptAnalyzer
Variables, defined with "-Option AllScope", in Nested Function reports "assigned but never used".
還沒有人認領這個 Issue。
- 主要語言
- C#
- 星號
- 2.2k
- 分支
- 415
- 平均合併
- 13 小時 1 分鐘
- 30 天內合併 PR
- 2
描述
Issue Description
I'm learning PowerShell, and also new to Visual Studio Code, and ran into a situation where it would be useful to create nested functions that access the parent function's variables. To my pleasant surprise PowerShell can do this and I created the below code to try it out. But Visual Studio Code continues to complain "The variable 'Data' is assigned but never used." and "The variable 'Return' is assigned but never used." But PowerShell itself seems to be quite happy with the code and produces the results I expected.
It appears that Visual Studio code is not recognizing the work that "Set-Variable", with the "AllScope" option, does in making these variables accessible throughout the GetMatchingIndex method (I wouldn't be surprised if "AllScope" makes it accessible outside the class - coming from a C# background, PowerShell does some really weird things).
If VSCode is working correctly, and there really are 2 bugs in my code, then I guess I need someone to explain how to fix the code.
class FERMI { # ForEachReturnMatchingIndex
[string[]]$CharSets
FERMI() {
$this.CharSets = 'ab', 'cd', 'ef', 'gh'
}
[int] GetMatchingIndex([string]$StringToFind) {
Write-Host
Set-Variable -Name ItemIndex, UpperOuterBounds, Data, Return -Option AllScope
[int]$Return = [int]$ItemIndex = -1
$UpperOuterBounds = $this.CharSets.Length
function AbleToGetAnotherItem {
[OutputType([bool])]
param ()
if ($atgaiReturn = ++$ItemIndex -lt $UpperOuterBounds) { $Data = $this.CharSets[$ItemIndex]}
return $atgaiReturn
}
function ProcessData {
Write-Host "Data: $Data, Index: $ItemIndex"
if($Data -eq $StringToFind){
$Return = $ItemIndex
}
}
While (AbleToGetAnotherItem) {
ProcessData
}
return $Return
}
}
[FERMI] $MyFERMI = [FERMI]::New()
Write-Host "ef: $($MyFERMI.GetMatchingIndex('ef'))"
Write-Host "cd: $($MyFERMI.GetMatchingIndex('cd'))"
Write-Host "ij: $($MyFERMI.GetMatchingIndex('ij'))"
Write-Host "gh: $($MyFERMI.GetMatchingIndex('gh'))"
Write-Host "ab: $($MyFERMI.GetMatchingIndex('ab'))"
Environment Information
Visual Studio Code
| Name | Version |
|---|---|
| Operating System | Windows_NT x64 10.0.19042 |
| VSCode | 1.55.2 |
| PowerShell Extension Version | 2021.2.2 |
PowerShell Information
| Name | Value |
|---|---|
| PSVersion | 7.1.3 |
| PSEdition | Core |
| GitCommitId | 7.1.3 |
| OS | Microsoft Windows 10.0.19042 |
| Platform | Win32NT |
| PSCompatibleVersions | 1.0 2.0 3.0 4.0 5.0 5.1.10032.0 6.0.0 6.1.0 6.2.0 7.0.0 7.1.3 |
| PSRemotingProtocolVersion | 2.3 |
| SerializationVersion | 1.1.0.1 |
| WSManStackVersion | 3.0 |
Visual Studio Code Extensions
Visual Studio Code Extensions(Click to Expand)
| Extension | Author | Version |
|---|---|---|
| powershell | ms-vscode | 2021.2.2 |
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先,使用報告中的 PowerShell 與 VS Code 擴充功能版本執行提供的 FERMI 重現步驟,然後檢查 analyzer 如何追蹤在巢狀函式中透過 Set-Variable -Option AllScope 建立的變數。完成的標準是:診斷能正確計入這些變數的使用情況,或是 issue 清楚記錄警告為何有效。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- powershell, vscode
- 領域
- tooling
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100