Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implemented feedback from CoPilot
  • Loading branch information
iRon7 committed Jun 10, 2026
commit 0aad726963f6b89c11943a4a44c28c63af979e33
20 changes: 15 additions & 5 deletions Rules/AvoidUsingNewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
/// The rule implements a correction that suggests using type-casting or type constructor.
///
/// Note:
/// In case a automatic correction isn't available, the rule won't report any violation either.
/// This is because if there isn't an automatic correction available, it means that the there
/// isn't a simple type-casting or type constructor that can be used as a replacement that
/// would be more efficient or has a better syntax than using New-Object.
/// In other words, if the `-ComObject` parameter is used, or both the parameters
/// In most cases if there isn't an automatic correction isn't available,
Comment thread
iRon7 marked this conversation as resolved.
Outdated
/// the rule won't report any violation either.
/// This is because if there isn't an automatic correction available, it generally means
/// that there isn't a simple type-casting or type constructor that can be used that would
/// be more efficient or has a better syntax than using New-Object.
/// In other words, if the common `-Verbose` parameter is used, or both the parameters
/// `-ArgumentList` and `-Property` are used, there won't be a simple type initializer
/// available and the rule won't report any violation for the `New-Object` cmdlet.
///
/// Nevertheless, there are still some cases where the `New-Object` cmdlet might be
/// replaceable with a type initializer that would be more efficient or has a better syntax,
/// but an automatic correction can't be provided.
/// For example if the `-ArgumentList` parameter is used with a variable,
/// the rule will report a violation, but won't be able to provide a correction,
/// as it's not possible to determine from the AST alone whether the variable contains a
/// single value that can be used in a type initializer,
/// or if it contains multiple values that would require splatting.
/// </summary>
Comment thread
iRon7 marked this conversation as resolved.
public class AvoidUsingNewObject : ConfigurableRule
{
Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/AvoidUsingNewObject.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ BeforeDiscovery {
$errors = $Null
$null = [Parser]::ParseInput($ActualValue, [ref]$null, [ref]$errors)
$succeeded = -not $errors -xor $Negate
if ($succeeded) {
if (-not $succeeded) {
$not = if ($Negate) { ' not' }
$failureMessage = "Expected '$ActualValue' to$Not parse$(if($Because) { " because $Because"})."
$failureMessage = "Expected '$ActualValue'$Not to parse$(if($Because) { " because $Because"})."
}

return [PSCustomObject]@{
Expand Down
4 changes: 2 additions & 2 deletions docs/Rules/AvoidUsingNewObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ for ($i = 0; $i -lt 100000; $i++) {
```

Comment thread
iRon7 marked this conversation as resolved.
```powershell
$hashSet = [System.Collections.Generic.HashSet[String]]([StringComparer]::InvariantCultureIgnoreCase)
$hashSet = [System.Collections.Generic.HashSet[String]]::new([StringComparer]::InvariantCultureIgnoreCase)
```

## Configuration

Comment thread
iRon7 marked this conversation as resolved.
```powershell
Rules = @{
PSAvoidNewObject = @{
PSAvoidUsingNewObject = @{
Enable = $true
}
}
Expand Down