Skip to content

Fixes #17 - Update description of rule #74

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

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Extra Variables
ms.custom: PSSA v1.20.0
ms.date: 10/18/2021
ms.custom: PSSA v1.21.0
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.'
}
```