Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upReviewUnusedParameter does not capture parameter usage within a scriptblock #1472
Comments
|
@mattmcnabb @rjmholt It seems we actually discussed this in the PR review with divided opinions: #1382 (comment) |
|
This caused some of my CI pipelines to suddenly fail today. Since I hadn't pinned the version of PSScriptAnalyzer that was used in the pipelines, and since I hadn't updated to 1.19.0 locally, I thought something was wrong with the pipeline or that I'd inadvertently introduced an error to our scripts. As a user I'd expect to receive a warning only if there really is an issue. I like the idea of the rule, but I'd rather forgo it entirely than have to add workarounds to our scripts or toggle In case I'm not understanding what the (This is my first time commenting on a PSScriptAnalyzer issue or PR. -Even though this new rule has been negative for me, I want to thank you for your work on this tool--it's been a great help not only for ensuring a clear and consistent style for our scripts but also for teaching how to use PowerShell.) |
|
We can theoretically know in this case that the variable will be used; However, this is going to be undecidable in general, since I can write a program like this: function New-ScriptBlock
{
param($x)
{ "`$x: $x" }
}
$sb = New-ScriptBlock -x 'Hi'
& $sbWe can solve the common case problem for commands that pass and invoke scriptblocks, but the blocker there is parameter binding; to properly resolve when a scriptblock corresponds to a parameter that's going to be invoked immediately, we really need a general purpose way to decide which argument corresponds to which parameter. That's where I got to here; it's not just that we need to solve it for |
|
I think for now it's ok to search nested scriptblocks though |
|
There is also this proposal in PowerShell to help PSSA: PowerShell/PowerShell#12287 |
|
@kmbn The idea behind |
Yeah, this proposal would help in the cases we don't know about, but most of the time people use In this case, the simple solution is to also look in the child scriptblock. The better solution is to search the child scriptblock when the command is |
See See PowerShell/PSScriptAnalyzer#1472 Co-authored-by: Christoph Bergmeister [MVP] <c.bergmeister@gmail.com>
See See PowerShell/PSScriptAnalyzer#1472 Co-authored-by: Christoph Bergmeister [MVP] <c.bergmeister@gmail.com>
See See PowerShell/PSScriptAnalyzer#1472 Co-authored-by: Christoph Bergmeister [MVP] <c.bergmeister@gmail.com>
See See PowerShell/PSScriptAnalyzer#1472 Co-authored-by: Christoph Bergmeister [MVP] <c.bergmeister@gmail.com>
To add to the list, we're also seeing this fail with |
|
@JPRuskin The rule looks only in the current scope at the moment, which is something we should definitely improve as per above comments. Thanks for the suggestion to also scan for
|
Somehow a number of PSScriptAnalyzer issues snuck in. This fixes them.
The main PSScriptAnalyzer issues needing to be fixed were:
* `PSReviewUnusedParameter` - This one came up a lot due to our heavy
usage of `Resolve-RepositoryElements` and `Resolve-ParameterWithDefaultConfigurationValue`
which both end up referencing their parameters by grabbing them off
the stack. That means that `NoStatus` and `Uri` are frequently
never directly referenced. So, exceptions were added. There were
two cases (in GitHubAnalytics) where there was a false positive due
to PowerShell/PSScriptAnalyzer#1472
* `PSUseProcessBlockForPipelineCommand` - We had a number of functions
that took pipeline input, but didn't actuall use the `process` block.
This actually caught a bug with `Group-GitHubIssue` and
`Group-GitHubPullRequest`. Added correct `process` block usage for
most of the functions, but removed pipeline support for those where
it didn't actually make sense anymore.
* `PSUseDeclaredVarsMoreThanAssignments` - These are false positives
in the Pester tests due to the usage of `BeforeAll`. There wasn't
an obvious way to use `SuppressMessageAttribute` in the Pester test,
so I used a hacky workaround to "use" the variable in the `BeforeAll`
block. I could have added the suppression to the top of the file,
but I still want to catch real issues in those files later.
Also, it turns out that `Group-GitHubPullRequest` hadn't actually been
exported, so I fixed that too.
Somehow a number of PSScriptAnalyzer issues snuck in. This fixes them.
The main PSScriptAnalyzer issues needing to be fixed were:
* `PSReviewUnusedParameter` - This one came up a lot due to our heavy
usage of `Resolve-RepositoryElements` and `Resolve-ParameterWithDefaultConfigurationValue`
which both end up referencing their parameters by grabbing them off
the stack. That means that `NoStatus` and `Uri` are frequently
never directly referenced. So, exceptions were added. There were
two cases (in GitHubAnalytics) where there was a false positive due
to PowerShell/PSScriptAnalyzer#1472
* `PSUseProcessBlockForPipelineCommand` - We had a number of functions
that took pipeline input, but didn't actuall use the `process` block.
This actually caught a bug with `Group-GitHubIssue` and
`Group-GitHubPullRequest`. Added correct `process` block usage for
most of the functions, but removed pipeline support for those where
it didn't actually make sense anymore.
* `PSUseDeclaredVarsMoreThanAssignments` - These are false positives
in the Pester tests due to the usage of `BeforeAll`. There wasn't
an obvious way to use `SuppressMessageAttribute` in the Pester test,
so I used a hacky workaround to "use" the variable in the `BeforeAll`
block. I could have added the suppression to the top of the file,
but I still want to catch real issues in those files later.
* `PSAvoidOverwritingBuiltInCmdlets` - It turns out that there's a bug
with PSDesiredStateConfiguration in PS Core 6.1.0 where it was exporting
internal functions. This was thus a false-postive flag for Write-Log.
See PowerShell/PowerShell#7209 for more info.
Also, it turns out that `Group-GitHubPullRequest` hadn't actually been
exported, so I fixed that too.
Somehow a number of PSScriptAnalyzer issues snuck in. This fixes them.
The main PSScriptAnalyzer issues needing to be fixed were:
* `PSReviewUnusedParameter` - This one came up a lot due to our heavy
usage of `Resolve-RepositoryElements` and `Resolve-ParameterWithDefaultConfigurationValue`
which both end up referencing their parameters by grabbing them off
the stack. That means that `NoStatus` and `Uri` are frequently
never directly referenced. So, exceptions were added. There were
two cases (in GitHubAnalytics) where there was a false positive due
to PowerShell/PSScriptAnalyzer#1472
* `PSUseProcessBlockForPipelineCommand` - We had a number of functions
that took pipeline input, but didn't actuall use the `process` block.
This actually caught a bug with `Group-GitHubIssue` and
`Group-GitHubPullRequest`. Added correct `process` block usage for
most of the functions, but removed pipeline support for those where
it didn't actually make sense anymore.
* `PSUseDeclaredVarsMoreThanAssignments` - These are false positives
in the Pester tests due to the usage of `BeforeAll`. There wasn't
an obvious way to use `SuppressMessageAttribute` in the Pester test,
so I used a hacky workaround to "use" the variable in the `BeforeAll`
block. I could have added the suppression to the top of the file,
but I still want to catch real issues in those files later.
* `PSAvoidOverwritingBuiltInCmdlets` - It turns out that there's a bug
with PSDesiredStateConfiguration in PS Core 6.1.0 where it was exporting
internal functions. This was thus a false-postive flag for Write-Log.
See PowerShell/PowerShell#7209 for more info.
Also, it turns out that `Group-GitHubPullRequest` hadn't actually been
exported, so I fixed that too.
|
I have found another way in which this rule incorrectly fires which is related to this issue: Consider this: function get-DoesContainScheme {
[OutputType([boolean])]
param(
[Parameter()]
[string]$SchemeName,
[Parameter()]
[object[]]$Schemes
)
# $null = $SchemeName;
$found = $Schemes | Where-Object { $_.name -eq $SchemeName };
return ($null -ne $found);
}Reports:
... and for completeness:
|
|
This issue is also observed in Pester tests when you directly use Parameter in "It" statement. |
|
@bergmeister I have tried suppressing the warning as you suggested:
But I am still getting the PSScriptAnalyzer warning. What am I doing wrong? Do I need to suppress it at the cmdlet level for the specific parameter?, ie
I dunno, I've tried several things and none of them are working, and I'm not seeing any real documentation on it. |
Steps to reproduce
Run
Invoke-ScriptAnalyzeragainst the following with the new 1.19.0 release.Expected behavior
No rule violations.
Actual behavior
The new
ReviewUnusedParameterrule doesn't notice the usage. I suspect this is similar to the limitation of theAvoidUsingCmdletAliasesrule though. Not sure if we should relax theReviewUnusedParameterrule in this case to search nested scriptblocks inside a function scope.cc @mattmcnabb @rjmholt @JamesWTruher
If an unexpected error was thrown then please report the full error details using e.g.
$error[0] | Select-Object *Environment data