PSReviewUnusedParameter does not look in scriptblocks
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
function f {
Param($param)
{ $param }
}
Expected behavior
No warnings.
Actual behavior
$param
has an PSReviewUnusedParameter warning.
Environment data
Name Value
---- -----
PSVersion 7.1.2
PSEdition Core
GitCommitId 7.1.2
OS Linux 5.10.11-v7+ #1399 SMP Thu Jan 28 12:06:05 GMT 2021
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
1.19.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:5 (3 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 >functions/utility/Get-PSFScriptblock.ps1 1.7.237
Filter scriptblocks by their tags. This can be further filtered by using a wildcard supporting string as -Name. .PARAMETER Description
Read more >When is a ScriptBlock not a ScriptBlock?
After some experimentation, I did find a workaround. If I modify Start-Test so that the type of the $Block parameter is [string] instead...
Read more >A Look at Implementing $Using: Support in PowerShell for ...
Here we have our data and then our script block which contains our $Using: variables.
Read more >Understanding PowerShell Scriptblocks
This cmdlet essentially "converts" a "non-expression" like a string to an expression. The string write-output 'foo' won't do anything at all ...
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
This is a tricky one, related to https://github.com/PowerShell/PSScriptAnalyzer/issues/1641.
In your particular example, that parameter is indeed unused. The scriptblock referencing a variable of the same name is not invoked within the scope of that parameter. Instead, the scriptblock will be passed out of the function for later invocation when dynamic scope will mean it will pick up a different value of
$param
based on the call stack at that time; PowerShell scriptblocks are not closures, so the value of the$param
parameter is not exported with the scriptblock returned by this function.However, in this scenario:
$param
is being used.However a scenario like that is unusual.
Much more common is something like:
However, solving this is non-trivial because we (1) must build a list of cmdlets that implicitly invoke their arguments within the current scope and (2) must work out special rules for all of them including a good way of working out how their parameters are bound.
For example compare:
with:
Same command, but the scriptblock invocation semantics are very different, and ideally the rule would fire in the second but not the first case.
That requires a fair amount of work.
This issue has been marked as duplicate and has not had any activity for 1 day. It will be closed for housekeeping purposes.