PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.87k stars 378 forks source link

`SuppressMessageAttribute` not working for ScriptBlock params #1861

Open totkeks opened 2 years ago

totkeks commented 2 years ago

Before submitting a bug report:

Steps to reproduce

Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', 'wordToComplete', Justification = 'Reason for suppressing')]
    param(
        $wordToComplete,
        $commandAst,
        $cursorPosition)

    dotnet complete --position $cursorPosition "$commandAst" | ForEach-Object {
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
    }
}

Expected behavior

Rule `PSReviewUnusedParameter` doesn't trigger for parameter `$wordToComplete`.

Actual behavior

Rule `PSReviewUnusedParameter` triggers for parameter `$wordToComplete`.

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.22000
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() }
Using built-in version of VSCode Powershell extension (v2022.10.0)
bergmeister commented 2 years ago

The documentation here says: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer?view=ps-modules#suppressing-rules

Within the scope of the script, function, or parameter that you decorated, all rule violations are suppressed.

Therefore, this is currently by design. You need to either do this on the function where this line is or within the script file by putting a param() block at the top.

totkeks commented 2 years ago

Thanks for the reply! Putting the param() block in the script file with all the argument completers works nicely and is appropriate since all of them have those three params.

Could we use this issue as a feature request for adding it to script blocks or should I create a separate feature request?