You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting the CheckID of a SuppressMessageAttribute decoration to '_' fails to suppress the specified message about the '_' automatic variable.
Specifically, this fails: [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '_', Justification='Assignment is by design.')]
While this works as expected: [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification='Assignment is by design.')]
Steps to reproduce
Run Invoke-ScriptAnalyzer against the following PowerShell script.
functionImport-StringData {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable','_', Justification='Assignment is by design.')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','newEntryTemplate', Justification='The subsequent use of newEntryTemplate is not detected by PSScriptAnalyzer.')]
Param([string]$stringToParse)
#region Prepare CrossRef Table
[System.Data.DataTable]$dt=New-Object System.Data.DataTable
$dt.Columns.Add("measCode", [string]) |Out-Null$dt.Columns.Add("measBasis", [string]) |Out-Null$dt.Rows.Add(@("BZ","MMBtu")) |Out-Null$dt.Rows.Add(@("G8","Gigacalories")) |Out-Null$dt.Rows.Add(@("GV","Gigajoules")) |Out-Null$dt.Rows.Add(@("TZ","MCF")) |Out-Null$dt.AcceptChanges()
#endregion Prepare CrossRef Table$stringToParse|Select-Object-First 7|ForEach-Object-Begin {$newEntryTemplate="`r`n{0}={1}"} -Process {
$match= ($_|Select-String"Meas Basis Desc[:] (.*)"-List).Matches
if($null-ne$match){$_+=$newEntryTemplate-f"measCode",$dt.Select("measBasis='$($match.Groups[1].Value)'").measCode}
$_-replace": ","="} |ConvertFrom-StringData
}
$stringToParse=@"Posting Date: 2020-09-17Posting Time: 22:05:44 Effective Gas Day: 2020-09-18Effective Gas Time: 9:00:00 am Meas Basis Desc: MMBtu"@Import-StringData$stringToParse
Expected behavior
Invoke-ScriptAnalyzer should find 0 rule violations.
The PSAvoidAssignmentToAutomaticVariable warning about the '_' variable on line 18 should be suppressed as per the targeted SuppressMessageAttribute decoration on line 2.
Actual behavior
The SuppressMessageAttribute decoration on line 2 has no effect and Invoke-ScriptAnalyzer outputs the following:
Invoke-ScriptAnalyzer: Suppression Message Attribute error at line 2 in Test PSScriptAnalyzer.ps1 : Cannot find any DiagnosticRecord with the Rule Suppression ID _.
RuleName
Severity
ScriptName
Line
Message
PSAvoidAssignmentToAutomaticVariable
Warning
Test Suppression.ps1
18
The Variable '_' is an automatic variable that is built into PowerShell, assigning to it might have undesired side effects. If assignment is not by design, please use a different name.
Thanks @BinaryWizard904 for the detailed information...what you are expecting is not something we support today--nothing ever diagnostic constructor for this rule does not take a Rule Suppression ID (suppression ID is on a rule by rule basis) so I will go ahead and mark this as a feature request...thanks!
…ic variable or parameter (#1896)
* add ruleId to PSAvoidAssignmentToAutomaticVariable
Enables suppression for specific variable/parameter.
Fix#1589
* add tests
Setting the CheckID of a
SuppressMessageAttribute
decoration to '_' fails to suppress the specified message about the '_' automatic variable.Specifically, this fails:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '_', Justification='Assignment is by design.')]
While this works as expected:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', '', Justification='Assignment is by design.')]
Steps to reproduce
Run
Invoke-ScriptAnalyzer
against the following PowerShell script.Expected behavior
Invoke-ScriptAnalyzer
should find 0 rule violations.The
PSAvoidAssignmentToAutomaticVariable
warning about the '_' variable on line 18 should be suppressed as per the targetedSuppressMessageAttribute
decoration on line 2.Actual behavior
The
SuppressMessageAttribute
decoration on line 2 has no effect andInvoke-ScriptAnalyzer
outputs the following:Environment data
The text was updated successfully, but these errors were encountered: