PowerShell / PSScriptAnalyzer

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

Testing PSSA Warning Rules.PSUseLiteralInitializerForHashtable #1984

Closed kaiaschulz closed 8 months ago

kaiaschulz commented 8 months ago

Before submitting a bug report:

Steps to reproduce

scriptName_Parallel.ps1:

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')]

$htName2 = [System.Collections.Hashtable]::Synchronized((New-Object System.Collections.Hashtable))

scriptName.ps1

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')]

$htName = @{}

Expected behavior

I wouldn't expect that I am getting these messaged as I am trying to supress them with [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')] and using the correct way of Create the hashtable using a literal hashtable expression.

Actual behavior

##[error] [-] Testing PSSA Warning Rules.PSUseLiteralInitializerForHashtable 101ms (99ms|2ms)
##[error]  Expected $null or empty, but got @('Problem in scriptName_Parallel.ps1 at line 708 with message: Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default', 'Problem in scriptName_Parallel.ps1 at line 793 with message: Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default', 'Problem in scriptName.ps1 at line 429 with message: Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default').
##[error]  at Should -BeNullOrEmpty, /home/vsts/work/1/s/tests/advanced/PSSA.tests.ps1:24
##[error]  at <ScriptBlock>, /home/vsts/work/1/s/tests/advanced/PSSA.tests.ps1:22

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

Environment data

> $PSVersionTable
Azure DevOps pipeline:
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.236.2
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/7832ab07-1625-468c-aa49-980af215e8a5.ps1'
VERBOSE: Populating RepositorySourceLocation property for module Pester.
VERBOSE: Populating RepositorySourceLocation property for module PSScriptAnalyzer.

    STATUS: Testing with PowerShell 7.4.1
    STATUS: Testing with Pester 5.5.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
    STATUS: Testing with Pester 5.5.0
liamjpeters commented 8 months ago

Hey @kaiaschulz,

Powershell attributes let you attach additional information to classes, functions, and properties.

PSScriptAnalyzer checks for the suppression attribute applied to ScriptBlock param block, Function param block, Class type definitions, and DSC Configurations.

Your example scripts are applying the attribute to the hashtable itself - which doesn't seem valid in this case.

You can put a param() block at the top of your file and apply the attribute to that.

You will also need to remove the errant space at the end of the rule name. You currently have 'PSUseLiteralInitializerForHashtable '.

The below correctly supresses the PSUseLiteralInitializerForHashtable rule.

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable', '', Justification = 'PSUseLiteralInitializerForHashtable ')]
param()

$htName2 = [System.Collections.Hashtable]::Synchronized((New-Object System.Collections.Hashtable))

Hope that helps 😀!

kaiaschulz commented 8 months ago

Hi @liamjpeters, thanks a lot for the detailed description and links for details. After you responded with that, I found that I had some trailing whitespaces which were causing the issue: Wrote: [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable ', '', Justification = 'PSUseLiteralInitializerForHashtable ')]

Right: [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseLiteralInitializerForHashtable', '', Justification = 'PSUseLiteralInitializerForHashtable')]

I'm ashamed that I overlooked that. sorry