Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Cannot Suppress Messages About a Specific Automatic Variable #1589

Copy link
Copy link
@marshallwp

Description

@marshallwp
Issue body actions

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.

function Import-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-17
Posting Time: 22:05:44    
Effective Gas Day: 2020-09-18
Effective 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.

1 rule violation found. Severity distribution: Error = 0, Warning = 1, Information = 0

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.