File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed
Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change 11---
22description : Extra Variables
33ms.custom : PSSA v1.21.0
4- ms.date : 10/18/2021
4+ ms.date : 06/30/2022
55ms.topic : reference
66title : UseDeclaredVarsMoreThanAssignments
77---
@@ -11,8 +11,11 @@ title: UseDeclaredVarsMoreThanAssignments
1111
1212## Description
1313
14- Generally variables that are not used more than their assignments are considered wasteful and not
15- needed.
14+ Variables that are assigned but not used are not needed.
15+
16+ > [ !NOTE]
17+ > For this rule, the variable must be used within the same scriptblock that it was declared or it
18+ > won't be considered to be "used".
1619
1720## How
1821
@@ -40,3 +43,20 @@ function Test
4043 Write-Output $declaredVar
4144}
4245```
46+
47+ ### Special case
48+
49+ The following example triggers the ** PSUseDeclaredVarsMoreThanAssignments** warning because ` $bar `
50+ is not used within the scriptblock where it was defined.
51+
52+ ``` powershell
53+ $foo | ForEach-Object {
54+ if ($_ -eq $false) {
55+ $bar = $true
56+ }
57+ }
58+
59+ if($bar){
60+ Write-Host 'Collection contained a false case.'
61+ }
62+ ```
You can’t perform that action at this time.
0 commit comments