File tree 1 file changed +24
-4
lines changed
reference/docs-conceptual/PSScriptAnalyzer/Rules
1 file changed +24
-4
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
description : Extra Variables
3
- ms.custom : PSSA v1.20 .0
4
- ms.date : 10/18/2021
3
+ ms.custom : PSSA v1.21 .0
4
+ ms.date : 06/30/2022
5
5
ms.topic : reference
6
6
title : UseDeclaredVarsMoreThanAssignments
7
7
---
@@ -11,8 +11,11 @@ title: UseDeclaredVarsMoreThanAssignments
11
11
12
12
## Description
13
13
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".
16
19
17
20
## How
18
21
@@ -40,3 +43,20 @@ function Test
40
43
Write-Output $declaredVar
41
44
}
42
45
```
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