-
Notifications
You must be signed in to change notification settings - Fork 49
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 - mention scoping #17
Comments
@bergmeister Can you comment on this issue? I think the docs need a better description but I am wondering if the rule should be renamed to better describe what it does. |
@sdwheeler The rule's analysis is limited when it comes to variables definition and usage between different scopes. The author is correct that their example produces a false positive but there are also examples where the rule has some intelligence/knowledge between scopes such as in Because the rule name is used in PSSA settings files, a rename would be quite disruptive and even if we created an alias name I am not sure if this would just make things confusing? The name of the rule should be read as 'You should use your declared variables and not just assign them a value' but I agree as well that the rule name doesn't read very fluently. |
Hi, thank you for your reply. I think my first suggested change would be in the description line, "Generally variables that are not used more than their assignments are considered wasteful and not needed." I would rephrase it to "Generally variables that are not used are considered wasteful and not needed". I think my second suggested change is to add a "How to Fix" section like you have for some of the other Problems documentation (like UseLiteralInitializerForHashtable) where you mention that "a variable's usage is sometimes ignored if it is not used in the same scope in which it was declared. Try using it in the same scope." Something like that. |
It doesn't seem that variables have to be used more than their assignments; they can also be used the same number of times. You should clarify the language by saying something like "greater than or equal to their assignments".
You should also mention that the variable must be used within the same scope in which it was declared or else it won't be considered to be "used".
I have an example...
**$foo | for-eachobject {if($_ -eq $false){$bar = $true}}
if($bar){write-host "You are still going to get a PSUseDeclaredVarsMoreThanAssignments error even though you are using the bar variable here."}**
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: