Skip to content

Commit

Permalink
Sync changes from docs repo (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwheeler authored Jul 11, 2022
1 parent a83e8ac commit 323623d
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions docs/Rules/UseDeclaredVarsMoreThanAssignments.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Extra Variables
ms.custom: PSSA v1.21.0
ms.date: 10/18/2021
ms.date: 06/30/2022
ms.topic: reference
title: UseDeclaredVarsMoreThanAssignments
---
Expand All @@ -11,8 +11,11 @@ title: UseDeclaredVarsMoreThanAssignments

## Description

Generally variables that are not used more than their assignments are considered wasteful and not
needed.
Variables that are assigned but not used are not needed.

> [!NOTE]
> For this rule, the variable must be used within the same scriptblock that it was declared or it
> won't be considered to be "used".
## How

Expand Down Expand Up @@ -40,3 +43,20 @@ function Test
Write-Output $declaredVar
}
```

### Special case

The following example triggers the **PSUseDeclaredVarsMoreThanAssignments** warning because `$bar`
is not used within the scriptblock where it was defined.

```powershell
$foo | ForEach-Object {
if ($_ -eq $false) {
$bar = $true
}
}
if($bar){
Write-Host 'Collection contained a false case.'
}
```

0 comments on commit 323623d

Please sign in to comment.