PSReviewUnusedParameter with script parameter and function using it
See original GitHub issueBefore submitting a bug report:
- Make sure you are able to repro it on the latest released version
- Perform a quick search for existing issues to check if this bug has already been reported
Steps to reproduce
param(
$a
)
function f
{
echo $a
}
f
Expected behavior
No warnings
Actual behavior
Invoke-ScriptAnalyzer .\a.ps1
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSReviewUnusedParameter Warning a.ps1 2 The parameter 'a' has been declared but not used.
Environment data
> $PSVersionTable
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…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
ReviewUnusedParameter - PowerShell
This rule identifies parameters declared in a script, scriptblock, or function scope that have not been used in that scope.
Read more >Passing script parameters to a function in Powershell
In your function change the name to something other than $args and it should work. function new( $arguments ) { $arguments.Length if( ...
Read more >Functions/Platforms/Disable-PASPlatform.ps1 4.3.65
Deactivates a platform. .DESCRIPTION Disables, target, group or rotational group platform. .PARAMETER TargetPlatform. Specify if ID relates to Target platform
Read more >Parametersetname powershell multiple. But, there has to be a ...
Describes how to define and use parameter sets in advanced functions. ParameterSetName Switch ($PSCmdlet. 0. microsoft. There's nothing wrong with it but ...
Read more >Understanding Windows PowerShell function parameters
Parameters can be created for scripts and functions. They are typically enclosed in a param block defined with the param keyword followed by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The
$a
value being referenced inf
is not necessarily the$a
being passed in (because the$a
reference inf
, at runtime, looks up the runtime variable scope stack to determine what value to use), so in general this isn’t a usage of the$a
parameter and there are no other usages of$a
in the immediate scope, so PSSA considers the parameter unused.It happens that in your scenario, due to dynamic scope, it is used thanks to scope inheritance. But PSScriptAnalyzer is a static analyser; it’s impossible in general for it (or any static analyser) to be able to infer a variable usage that depends on dynamic scope inheritance. In addition to that, most of the time when a parameter is used in the way that you’ve used it, it’s because the author is expecting lexical scope rather than dynamic scope and this represents a bug in their script.
So because analysing variables from dynamic scope isn’t something that PSScriptAnalyzer can feasibly provide guarantees about, and because depending on dynamic scope like this is generally not something that PSScriptAnalyzer is trying to encourage, getting a warning about the
$a
variable in this scenario is expected. The warning message could probably be improved, but that’s part of a larger work item around improving the way the unused variable logic works in https://github.com/PowerShell/PSScriptAnalyzer/issues/1641.This is actually a valid warning. If you dot-source that script and call
f
like this:You’re going to see that second call of
f
output$null
.