Skip to content

Commit 56597c7

Browse files
authored
Update description of rule (#74)
1 parent b35d44d commit 56597c7

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

reference/docs-conceptual/PSScriptAnalyzer/Rules/UseDeclaredVarsMoreThanAssignments.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
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
55
ms.topic: reference
66
title: 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+
```

0 commit comments

Comments
 (0)