Skip to content

Commit bdff332

Browse files
author
Kapil Borle
committed
Add documentation for UseLiteralInitializerForHashtable rule
1 parent e925335 commit bdff332

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

RuleDocumentation/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
|[UseDeclaredVarsMoreThanAssignments](./UseDeclaredVarsMoreThanAssignments.md) | Warning|
3939
|[UseIdenticalMandatoryParametersDSC](./UseIdenticalMandatoryParametersDSC.md) | Error |
4040
|[UseIdenticalParametersDSC](./UseIdenticalParametersDSC.md) | Error |
41+
|[UseLiteralInitializerForHashtable](./UseLiteralInitializerForHashtable.md) | Warning |
4142
|[UseOutputTypeCorrectly](./UseOutputTypeCorrectly.md) | Information|
4243
|[UsePSCredentialType](./UsePSCredentialType.md) | Warning|
4344
|[UseShouldProcessCorrectly](./UseShouldProcessCorrectly.md) | Warning|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# UseLiteralInitializerForHashtable
2+
**Severity Level: Warning**
3+
4+
## Description
5+
Creating a hashtable by either `[hashtable]::new()` or `New-Object -TypeName hashtable` will create a hashtable wherein the keys are looked-up in a case-sensitive manner, unless an `IEqualityComparer` object is passed as an argument. However, PowerShell is case-insensitive in nature and it is best to create hashtables with case-insensitive key look-up. This rule is intended to warn the author of the case-sensitive nature of the hashtable if he/she creates a hashtable using the `new` member or the `New-Object` cmdlet.
6+
7+
## How to Fix
8+
Use the full cmdlet name and not an alias.
9+
10+
## Example
11+
### Wrong:
12+
``` PowerShell
13+
$hashtable = [hashtable]::new()
14+
```
15+
16+
### Wrong:
17+
``` PowerShell
18+
$hashtable = New-Object -TypeName hashtable
19+
```
20+
21+
### Correct:
22+
``` PowerShell
23+
$hashtable = @{}
24+
```

0 commit comments

Comments
 (0)