Skip to content

ReviewUnusedParameter does not capture parameters implicitly used to determine ParameterSetName #1527

Closed
@ThePoShWolf

Description

@ThePoShWolf

Steps to reproduce

Function Get-Thing {
    [cmdletbinding(
        DefaultParameterSetName = '1'
    )]
    param (
        [Parameter(
            ParameterSetName = '2'
        )]
        [switch]$ParamSet2,
        [Parameter(
            ParameterSetName = '3'
        )]
        [switch]$ParamSet3
    )
    Switch ($PSCmdlet.ParameterSetName) {
        '1' {
            # ParameterSet = 1
        }
        '2' {
            # ParameterSet = 2
        }
        '3' {
            # ParameterSet = 3
        }
    }
    # Get the thing
}

Check it with the default rules in PSScriptAnalyzer:

Invoke-ScriptAnalyzer -Path .\Get-Thing.ps1

RuleName                Severity ScriptName    Line Message
--------                -------- ----------    ---- -------
PSReviewUnusedParameter Warning  Get-Thing.ps1 9    The parameter 'ParamSet2' has been declared but not used.
PSReviewUnusedParameter Warning  Get-Thing.ps1 13   The parameter 'ParamSet3' has been declared but not used.

Is it objectively better to rewrite the function? Something like:

Function Get-Thing {
    [cmdletbinding(
        DefaultParameterSetName = '1'
    )]
    param (
        [Parameter(
            ParameterSetName = '2'
        )]
        [switch]$ParamSet2,
        [Parameter(
            ParameterSetName = '3'
        )]
        [switch]$ParamSet3
    )
    if ($ParamSet2.IsPresent) {
        # ParameterSet = 2
    } elseif ($ParamSet3.IsPresent) {
        # ParameterSet = 3
    } else {
        # ParameterSet = 1
    }
    # Get the thing
}

This is what I've been doing for the functions that have been caught with this error.

Expected behavior

I would expect the rule not to be triggered on this script.

Actual behavior

RuleName                Severity ScriptName    Line Message
--------                -------- ----------    ---- -------
PSReviewUnusedParameter Warning  Get-Thing.ps1 9    The parameter 'ParamSet2' has been declared but not used.
PSReviewUnusedParameter Warning  Get-Thing.ps1 13   The parameter 'ParamSet3' has been declared but not used.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.2
PSEdition                      Core
GitCommitId                    7.0.2
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

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

1.19.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions