Skip to content

PSScriptAnalyzer disapproves the construction of any case sensitive hash table #1385

Closed
@iRon7

Description

@iRon7

The UseLiteralInitializerForHashtable should prevent programmers to inconsiderately build hash tables with a default comparison other then the default case sensitive PowerShell behavior. It should not prevent a programmer from building a case sensitive hash table at all.
Hash tables are often used to handle program data but they might also be used to store custom input data (whether or not case sensitive) and the desired output might highly depend on the customer's used-case.

In other words, it is good that I get a warning on

$hashtable = [hashtable]::new()

and:

$hashtable = New-Object -TypeName hashtable

But if I well-considerate explicitly set the comparison mode, like:

$hashtable = [HashTable]::New(0, [StringComparer]::Ordinal)

I expect the warning to be suppressed.

see also: How to create a case sensitive Hashtable without generating a PSScriptAnalyzer warning?

Steps to reproduce

Invoke-ScriptAnalyzer -ScriptDefinition '[HashTable]::New(0, [StringComparer]::Ordinal)'

Expected behavior

No warnings

Actual behavior

Use literal initializer, @{{}}, for creating a hashtable as  they are case-insensitive by default

Environment data

PSScriptAnalyzer version: 1.18.3
Tested both Windows PowerShell (5.1) and PowerShell Core (6.2.3)

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