question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSReviewUnusedParameter does not look in scriptblocks

See original GitHub issue

Before 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:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rjmholtcommented, Feb 22, 2021

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($param)

& {
    "Hello $param"
}

$param is being used.

However a scenario like that is unusual.

Much more common is something like:

param($Value,, $Weight)

$Value | ForEach-Object { $sum = 0 } { $sum += $_ * $Weight } { $sum }

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:

param($X)

Invoke-Command { "Hello $X" }

with:

param($X)

Invoke-Command -ComputerName "MyOtherComputer" { "Hello $X" }

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.

0reactions
msftbot[bot]commented, Dec 16, 2021

This issue has been marked as duplicate and has not had any activity for 1 day. It will be closed for housekeeping purposes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found