PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Variables, defined with "-Option AllScope", in Nested Function reports "assigned but never used".

Open darinks opened this issue 4 years ago • 6 comments

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

darinks avatar Apr 27 '21 22:04 darinks

This is really an issue for PSScriptAnalyzer. See https://github.com/PowerShell/PSScriptAnalyzer/issues/1641.

rjmholt avatar Apr 27 '21 22:04 rjmholt

Robert,

Thank you for letting me know where this issue belongs. To be honest, one of my ToDos is learning GitHub. Is there a way for me to move the issue I opened to PSScriptAnalyzer? Or do I need to create a new issue in that section? And if the latter, then do I need to do something with the old issue I opened?

Thank you for your help,

Darin

On Tue, Apr 27, 2021 at 5:53 PM Robert Holt @.***> wrote:

This is really an issue for PSScriptAnalyzer. See #1641 https://github.com/PowerShell/PSScriptAnalyzer/issues/1641.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSScriptAnalyzer/issues/1669#issuecomment-827999666, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABU36FI4EGBIMFXUY3BQ2GLTK4567ANCNFSM43V4ZO7Q .

darinks avatar Apr 28 '21 13:04 darinks

Is there a way for me to move the issue I opened to PSScriptAnalyzer?

I've already moved it over — the issue and your reply are already in the PSScriptAnalyzer repo.

In terms of the actual issue itself, your usage is fairly unusual and hard to analyse, but given the explicit use of Set-Variable might be something that could be improved in the analyser.

I've marked it as up-for-grabs, since it's not a change the core team is likely to implement within the medium term.

rjmholt avatar Apr 28 '21 16:04 rjmholt

Robert,

Thank you for moving it over. As for my "fairly unusual" usage, I found another way of doing what I was trying. So this may be a case of "Just because you can do something doesn't mean you should." But I don't regret experimenting. Using Set-Variable in this way almost turns a function into a temporary virtual class with the nested functions as its methods. I may someday find a valid need for this,

Darin

On Wed, Apr 28, 2021 at 11:25 AM Robert Holt @.***> wrote:

Is there a way for me to move the issue I opened to PSScriptAnalyzer?

I've already moved it over — the issue and your reply are already in the PSScriptAnalyzer repo.

In terms of the actual issue itself, your usage is fairly unusual and hard to analyse, but given the explicit use of Set-Variable might be something that could be improved in the analyser.

I've marked it as up-for-grabs, since it's not a change the core team is likely to implement within the medium term.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSScriptAnalyzer/issues/1669#issuecomment-828593044, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABU36FN4WAKX5A2CUNCO5RDTLAZGFANCNFSM43V4ZO7Q .

darinks avatar Apr 28 '21 19:04 darinks

Yeah, no reason not to experiment. This is one of those cases where PSScriptAnalyzer might theoretically be able to, with some work, implement a more correct analysis, but really it should also have another rule specifically to say that there are other reasons not to do it.

In this case, PSSA probably should warn you not to use Set-Variable/Get-Variable/New-Variable.

rjmholt avatar Apr 28 '21 21:04 rjmholt

I like the idea of a warning. It would be good to get the person's attention on the code so they consider alternatives.

darinks avatar Apr 28 '21 21:04 darinks