Skip to content
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

PSUseDeclaredVarsMoreThanAssignments is not aware of the scriptblock context (invocation or dot-sourcing) #938

Open
bergmeister opened this issue Mar 15, 2018 · 1 comment

Comments

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 15, 2018

Steps to reproduce

Extracted from here but similar issues also reported in #711

It’s a PSSA bug. I get the same warning with:

$totalSize = 0
. { $totalSize += 1 }
$totalSize

PSSA is apparently analyzing each script block independently.

It needs to recognize when a script block is dot sourced and analyze it in the context of where it is >invoked. This isn’t always possible, but it’s usually easy to do when using ForEach-Object (which dot >sources like this).

In trying some variants on the idea, it seems like there are multiple bugs:

$totalSize = 0
function foo {
$x = $totalSize + 1
$totalSize = $x # Should warn, doesn't (bug)
}
& {
$x = $totalSize + 1
$totalSize = $x # Should warn, doesn't (bug)
}
. {
$x = $totalSize + 1
$totalSize = $x # Should not warn, doesn't (no bug)
}
. {
$totalSize += 1 # Should not warn, does (bug)
}
$totalSize
@JohnLBevan
Copy link

FYI: The same bug occurs in a slightly different use-case:

(2, 5, 16) | ForEach-Object `
     -Begin { [int]$runningTotal = 0 } `
     -Process { $runningTotal=$runningTotal -bor $_ } `
     -End { $runningTotal }

I guess this may be harder to cater for the above, since without knowing the function's internal use of the script block parameters it's very hard to say whether they're related / valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants