Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Suppress Messages About a Specific Automatic Variable #1589

Closed
marshallwp opened this issue Sep 18, 2020 · 1 comment · Fixed by #1896
Closed

Cannot Suppress Messages About a Specific Automatic Variable #1589

marshallwp opened this issue Sep 18, 2020 · 1 comment · Fixed by #1896

Comments

@marshallwp
Copy link

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
@SydneyhSmith
Copy link
Collaborator

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!

fflaten added a commit to fflaten/PSScriptAnalyzer that referenced this issue Feb 27, 2023
Enables suppression for specific variable/parameter.
Fix PowerShell#1589
bergmeister pushed a commit that referenced this issue Sep 17, 2023
…ic variable or parameter (#1896)

* add ruleId to PSAvoidAssignmentToAutomaticVariable

Enables suppression for specific variable/parameter.
Fix #1589

* add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants