Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silence a warning or even better if you know why: is assigned but never

Tags:

powershell

$global:ProjectName = $null

function RunFirst(){
  RunSecund
  Write-Host $global:ProjectName
}

function RunSecund(){
    $global:ProjectName = "a name"
}

In RunSecund I get:

The variable 'ProjectName' is assigned but never used. PSScriptAnalyzer(PSUseDeclaredVarsMoreThanAssignments)

like image 770
Chris G. Avatar asked Oct 27 '25 17:10

Chris G.


1 Answers

Easy, don't use global variables!

Although, if you must, you can suppress PSSA warnings with SuppressMessageAttribute:

function RunSecund(){
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Scope='Function')]
    $global:ProjectName = "a name"
}
like image 194
Mathias R. Jessen Avatar answered Oct 30 '25 08:10

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!