diff --git a/docs/Rules/UseDeclaredVarsMoreThanAssignments.md b/docs/Rules/UseDeclaredVarsMoreThanAssignments.md index bb5bfba69..3f66d7b03 100644 --- a/docs/Rules/UseDeclaredVarsMoreThanAssignments.md +++ b/docs/Rules/UseDeclaredVarsMoreThanAssignments.md @@ -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 --- @@ -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 @@ -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.' +} +```