PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.8k stars 365 forks source link

Private parameters are reported as unused #1891

Open papadeltasierra opened 1 year ago

papadeltasierra commented 1 year ago

Before submitting a bug report:

Steps to reproduce

Run the analyzer against this script.

Param (
    [String] $private:var1
)

function Test-Second
{
    # $var1 is not accessible because it was private to the script.
    Write-Information -InformationAction "continue" "From script: $var1"

    # $var2 is not private to the calling function so we can access it.
    Write-Information -InformationAction "continue" "From caller: $var2"
}

function Test-NotGlobal
{
    Param (
        [String] $var2
    )
    $a = $var1 + " " + $var2
    Write-Information -InformationAction "continue" "Concatenation: $a"
    Test-Second
}

Test-NotGlobal $var1

Expected behavior

Expect no errors because $private:var1 and the use in the last line of $var1 are the same variable and $var1 is private to the script, as required.

Actual behavior

PS C:\Users\pauldsmith> Invoke-ScriptAnalyzer c:\temp\gvar.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSReviewUnusedParameter             Warning      gvar.ps1   2     The parameter 'private:var1' has been declared but not
                                                                  used.

PS C:\Users\pauldsmith>

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.963
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.963
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.21.0
bergmeister commented 1 year ago

This doesn't seem to happen with

$private:aa='sdf'
get-childitem $aa

So might be an easy improvement if you want to make that. But as the rule generally can result in lots of false positives due to it being limited to current scope, effort to such enhancements will be limited by the maintainers.

Piedone commented 4 months ago

I get the same violation in this script for all parameters, despite them being used in the functions. Is this the same issue, or a different one with the same output?

param
(
    $Summary,
    $Description,
    $Type,
    $IssueComponent,
    $LinkUrl,
    $LinkTitle
)

function CreateIssue
{
    $body = @{
        fields = @{
            project = @{
                key = $Env:JIRA_PROJECT_KEY
            }
            summary = $Summary
            description = $Description
            issuetype = @{
                name = $Type
            }
            labels = @('created-from-github')
        }
    }

    if (-not [string]::IsNullOrWhiteSpace($IssueComponent))
    {
        $body.fields += @{
            components = @(@{
                name = $IssueComponent
            })
        }
    }

    $bodyJson = $body | ConvertTo-Json -Depth 9

    $response = Invoke-JiraApiPost 'issue' $bodyJson

    Write-Information "Jira issue created with the key $($response.key)." -InformationAction Continue

    $response.key
}

function AddLink
{
    param($issueKey)

    $bodyJson =  @{
        object = @{
            url = $LinkUrl
            title = $LinkTitle
        }
    } | ConvertTo-Json -Depth 3

    Invoke-JiraApiPost "issue/$issueKey/remotelink" $bodyJson
}

$issueKey = CreateIssue
AddLink $issueKey
Set-GitHubOutput 'issue-key' $issueKey
Set-GitHubOutput 'issue-url' "$($Env:JIRA_BASE_URL )/browse/$issueKey"