-
Notifications
You must be signed in to change notification settings - Fork 389
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
false positive 'PSUseDeclaredVarsMoreThanAssignments' #711
Comments
Same here: Create an ADSI connection to the Service Account defined in the Main Window, and set the custom attribute (extensionAttribute) to the Entry #.
Check to see if there were any entries in the array after excluding the Entry #.If ($NewFileArray) { |
bump It'd be great to get this fixed - getting a lot of false positives in a couple of projects. |
Hey @kapilmb can you take a look and see how much effort it might take to get this resolved for an upcoming release? |
Fixing this rule requires some non-trivial amount of work. Right now the rule implementation is pretty ad-hoc. We would need to completely rewrite the rule to use analysis by single static assignment (SSA) form. One positive thing is that we do have a preliminary implementation of SSA, but it needs some modification. Will try to see to what I can do for the upcoming release but there is a good chance I won't make it by then. |
@daviwil When are we planning the next feature release? I might be able to make dash for this if there is some margin. |
I also see this when I declare a variable in a module and export it using Export-ModuleMember. |
bump |
I just noticed this bug... It appears any time a variable is assigned inside
OR
|
It is also triggered when you assign an environment variable like this. |
I'm seeing this when a variable is assigned a value inside specific script block types and no other reference is made within the same script block. I'm using VS Code 1.16.0 and ms-vscode.powershell 1.4.2. So far I've only seen two types of script blocks exhibiting the problem: ForEach-Object and Where-Object. Someone above mentioned arbitrary script blocks like
but I'm pretty sure it's working as designed, because it's got its own scope, so These ones are unaffected, though if they are wrapped in another, problematic script block type, the problem will still occur: if, else, elseif, do, while, try, catch, and finally. For example, the second instance of
|
I'm seeing this specifically with assigning values to collections inside a a EXAMPLE:
|
bump - Ex: Function MakeTrue {
$Global:g_GlobalVariable = $true
}
Function MakeFalse {
$Global:g_GlobalVariable = $false
} |
I have a variable declared within a function and then possibly modified in an conditional statement inside a loop, then finally returned by the function and I'm getting a false positive. If I move the return into the foreach-object loop then the message goes away (but obv that's not the behaviour I want from this function)
|
Same here with a Test code in the latest Describe 'incorrect configuration is corrected and registered' {
BeforeAll {
."$here\$sut" @testParams
$testPrintConfig = Get-PrintConfiguration -PrinterName $testPrinter.PrinterName
}
it 'Color' {
$testPrintConfig.Color | Should -Be $false
} -Skip:$Skip
it 'Collate' {
$testPrintConfig.Collate | Should -Be $false
} -Skip:$Skip
} |
Just reading through this issue to see if anyone's already asked for Pester variable special casing (which they have). I should note here that this rule will never be able to fully correctly detect when a variable isn't used in every case. There are several ways to go behind its back because of PowerShell's dynamic nature (dynamic meaning that the only way to know is to execute the code, which is potentially side-effectful -- this is known as "undecidable").
A few of these could be solved in easy cases where constant arguments are given, it's just a case of "shaving the yak". But as a static analyzer, PSScriptAnalyzer can only do so much. Ultimately this rule is supposed to be a helpful heuristic rather than an absolute. |
This might be covered by some of the other examples but just in case here is another sample where the |
That's a profound statement and I will taking this on board with relish! |
I get the message 'The variable 'param' is assigned but never used.'
My script looks like this:
a couple of lines further down in the same script:
The text was updated successfully, but these errors were encountered: