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

Question about 'PSUseDeclaredVarsMoreThanAssignment' rule #636

Closed
LaurentDardenne opened this issue Oct 13, 2016 · 7 comments
Closed

Question about 'PSUseDeclaredVarsMoreThanAssignment' rule #636

LaurentDardenne opened this issue Oct 13, 2016 · 7 comments

Comments

@LaurentDardenne
Copy link

This code triggers a violation :

$isduplicate=$false
Get-ChildItem c:\|
  Foreach-Object {
   $isDuplicate=$true
 }

This one too :

$isduplicate=$false
Get-ChildItem c:\|
  Foreach-Object {
   $isDuplicate=$true
 }
$isDuplicate
#true

But not this one :

$isduplicate=$false
Get-ChildItem c:\|
  Foreach-Object {
 }
$isDuplicate

It is the first violation correct ?

@kapilmb
Copy link

kapilmb commented Oct 24, 2016

The first violation is not correct but it is a due to the limitation of the rule's implementation - It doesn't inspect downstream scopes for a variable's use. The second case is a bug

@kapilmb kapilmb added this to the 1611 milestone Oct 24, 2016
@kapilmb kapilmb self-assigned this Oct 24, 2016
@kapilmb kapilmb modified the milestones: 1611, backlog Dec 2, 2016
@kapilmb
Copy link

kapilmb commented Dec 2, 2016

Moving this to backlog as the the current solution (in #655) to this problem is more of a patchwork and error prone. In order to solve this problem properly, we need to make use of techniques like SSA. PSSA currently has an implementation but that needs to be updated in order to use it in this context. Once we have that capability, we will fix this issue.

@it-praktyk
Copy link
Contributor

Probably the same issue is visible for code included in Pester project e.g. for the file build.psake.ps1

PSScriptAnalyzer output
psscriptanalyzer_pssake build ps1-0

and code

psscriptanalyzer_pssake build ps1-1

@markwragg
Copy link

I found today that $Variable += $Something triggers a PSUseDeclaredVarsMoreThanAssignments warning. Is that known?

Changing it to $Variable = $Variable + $Something works around it, but should += be causing this to trrigger?

@ryangribble
Copy link

I also encountered the $variable += $something triggering this violation.

Any comments about whether this is something that should be avoided or whether it's just not being understood properly and is a false positive?

@kapilmb
Copy link

kapilmb commented May 18, 2017

This is a false positive. $variable += $something is a perfectly valid construct but the rule is very naive and has a lot of limitations.

Sorry about not having a clear guidance as to when this will be fixed. We will push a fix soon, hopefully by the end of May or early June.

@rjmholt
Copy link
Contributor

rjmholt commented Feb 9, 2021

Duplicate of #1163

@rjmholt rjmholt marked this as a duplicate of #1163 Feb 9, 2021
@rjmholt rjmholt closed this as completed Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants