-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PSReviewUnusedParameter has false positive when using $MyInvocation.BoundParameters #1505
Comments
I don't see this as a false positive. You are not using the |
Hey @bergmeister , Hope you are doing good.
It is a switch, so I may never use that parameter and go with just parameterset name. You may wonder why would someone do this. Imagine a function that calls another function with bound parameter, like someone splat $PSBoundParameters to another function. |
I think pssa is doing exactly what can be expected from a static analysis tool; warn for suspect situations. Because the scenario you describe can not be statically analyzed in Powershell, I think there is little that could be done. This is my humble opinion and I may be wrong. |
@manigandan-jegannathan-developer I still don't understand why you cannot check the parameter directly, even when splatting with |
I appreaicte the effort on writing this rule, I am sure it would help many out there. I am trying to add my inputs so that we can improve this rule further. About that For example,
I can definitely use that parameter directly. I just wanted to note that it may flag when someone uses Also I just happened to notice that it flags if the parameter is being used in different runspace.
|
@manigandan-jegannathan-developer Aha, I see, if you use |
I just checked with someone and |
@JamesWTruher do you know if this is the case?
I would like to echo this sentiment. This doesn't necessarily mean that we shouldn't do something like turn off the rule when particular variables are used, but PowerShell leans heavily on dynamic features and I would prefer not to create the expectation that static analysis can do it all. In this case, it seems reasonable that we would detect a common scenario like this and disengage the rule, although we should document cases where the rule gives up. |
Yh, I agree, hence why I marked it as an enhancement instead of a bug. The enhancement is relatively easy, therefore it's also marked as up for grabs. We should look into a generic solution/improvement to the scoping issue though around child scriptblocks from which a few rules suffer and is probably the number one user annoyance in terms of false positives. |
I can take this up, but I never made any contribution to PSSA. Any advise or sample PR fixing similar small issue would be helpful |
Just look at the line that I linked before: https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Rules/ReviewUnusedParameter.cs#L49 |
Cool, Thanks. I will submit a PR. Thanks @bergmeister |
I am afraid whether that would work. |
Yh, you're right, it looked like a child to me first but is actually at the same level as |
{ param($x) $MyInvocation.BoundParameters }.Ast.FindAll({ $args[0] -is [System.Management.Automation.Language.MemberExpressionAst] -and $args[0].Expression -is [System.Management.Automation.Language.VariableExpressionAst] -and $args[0].Expression.VariablePath.UserPath -eq 'MyInvocation' -and $args[0].Member -is [System.Management.Automation.Language.StringConstantExpressionAst] -and $args[0].Member.Value -eq 'BoundParameters' }, $true) |
Thanks @rjmholt, that is exactly what I am doing. |
I have the changes ready, waiting for my firm to approve my OSS contribution. |
I have submitted a PR fixing this issue. It occurred to me that it should not flag $PScmdlet.MyInvocation.PSBoundParameters as well. So I made required changes to handle all three cases,
|
Steps to reproduce
There may be some rare cases where we would be using ParameterSetName instead of ParameterName, for example
Expected behavior
I expect this to be not flagged
Actual behavior
Flags it as violation
Environment data
The text was updated successfully, but these errors were encountered: