PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Setting `$ErrorView` is flagged as a warning: `PSUseDeclaredVarsMoreThanAssignment`

Open Kurt-von-Laven opened this issue 4 years ago • 2 comments

Steps to reproduce

$ErrorView = 'NormalView'

Expected behavior

No warning is issued for assignment without use of any preference variable. This is another variation of #651 and an instance of #1641.

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseDeclaredVarsMoreThanAssignment Warning      config.ps1 27    The variable
s                                                                 'ErrorView' i
                                                                  s assigned bu
                                                                  t never used.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.19041.1320
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1320
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
You cannot call a method on a null-valued expression.
At line:1 char:73
+ ... vailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
+                                                           ~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

I am running PSScriptAnalyzer via MegaLinter v5.0.7.

Kurt-von-Laven avatar Nov 16 '21 01:11 Kurt-von-Laven

Thanks @Kurt-von-Laven, we have reproduced this issue and are marking it up for grabs for a fix.

StevenBucher98 avatar Dec 07 '21 23:12 StevenBucher98

Yes, agree as well. This would be an easy fix by inserting an if statement here: https://github.com/PowerShell/PSScriptAnalyzer/blob/77ba74be0c6e8dc5c60f65bca1e5d3886c3964f8/Rules/UseDeclaredVarsMoreThanAssignments.cs#L215

bergmeister avatar Dec 08 '21 21:12 bergmeister