PowerShell / PowerShell/PSScriptAnalyzer
Variables, defined with "-Option AllScope", in Nested Function reports "assigned but never used".
まだ誰も着手していません。
- 主要言語
- C#
- スター
- 2.2k
- フォーク
- 414
- 平均マージ
- 13時間 1分
- マージ済み PR(30日)
- 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、報告されている PowerShell と VS Code 拡張機能のバージョンで、提供されている FERMI の再現手順を実行します。次に、ネストした関数内で Set-Variable -Option AllScope によって作成された変数を analyzer がどのように追跡しているかを調査します。診断が変数の使用を正しく考慮するか、警告が有効である理由が issue に明確に記載されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- powershell, vscode
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100