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

PSUseDeclaredVarsMoreThanAssignment in read(only) scope #1109

Closed
iRon7 opened this issue Dec 13, 2018 · 2 comments
Closed

PSUseDeclaredVarsMoreThanAssignment in read(only) scope #1109

iRon7 opened this issue Dec 13, 2018 · 2 comments

Comments

@iRon7
Copy link

iRon7 commented Dec 13, 2018

Steps to reproduce

I am not sure whether this is a duplicate of #636 and/or the implication is completely understood:

Function Get-A {$a}
Function Get-B {
	$a = 1
	Get-A
}
Get-B

Expected behavior

No reported issue

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      Analyzer.p 3     The variable 'a' is assigned but never used.
s                                                s1

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Used case:
https://github.com/iRon7/ConvertTo-Expression/blob/master/ConvertTo-Expression.ps1

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      ConvertTo- 208   The variable 'Space' is assigned but never used.
s                                                Expression
                                                 .ps1
PSUseDeclaredVarsMoreThanAssignment Warning      ConvertTo- 208   The variable 'LineUp' is assigned but never used.
s                                                Expression
                                                 .ps1

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.17134.407
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.407
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.17.1
@iRon7 iRon7 changed the title PSUseDeclaredVarsMoreThanAssignment in readonly scope PSUseDeclaredVarsMoreThanAssignment in read(only) scope Dec 13, 2018
@bergmeister
Copy link
Collaborator

bergmeister commented Dec 14, 2018

@iRon7 The rule implementation is limited in its analysis to a scriptblock, which is a function here, therefore it cannot see the 'usage'. Because PowerShell propagates all variables to lower scopes (i.e. when you call a function), one technically does not need to pass variables explicitly but it is highly discouraged from a code readability/maintainability perspective. Therefore I strongly suggest to add a parameter to your Get-A function so that you can see the flow of dependencies/arguments and this will also 'fix' the PSScriptAnalyzer warning.

@iRon7
Copy link
Author

iRon7 commented Dec 14, 2018

Thank you for the answer,
I think that I have oversimplified my mvce.
In my case, the concerned Get-A function has already a lot of parameters and the concerned variable $a merely is a public setting/mode (which less meaning) for every (recursive) function called down the line.

Anyways, I basically agree with your statement, but question whether there are any justified exceptions to this rule.
I have been thinking about this for quite some time and I do not know the answer...

@iRon7 iRon7 closed this as completed Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants