Skip to content

Commit 0aad726

Browse files
committed
Implemented feedback from CoPilot
1 parent e14adb2 commit 0aad726

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

Rules/AvoidUsingNewObject.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2323
/// The rule implements a correction that suggests using type-casting or type constructor.
2424
///
2525
/// Note:
26-
/// In case a automatic correction isn't available, the rule won't report any violation either.
27-
/// This is because if there isn't an automatic correction available, it means that the there
28-
/// isn't a simple type-casting or type constructor that can be used as a replacement that
29-
/// would be more efficient or has a better syntax than using New-Object.
30-
/// In other words, if the `-ComObject` parameter is used, or both the parameters
26+
/// In most cases if there isn't an automatic correction isn't available,
27+
/// the rule won't report any violation either.
28+
/// This is because if there isn't an automatic correction available, it generally means
29+
/// that there isn't a simple type-casting or type constructor that can be used that would
30+
/// be more efficient or has a better syntax than using New-Object.
31+
/// In other words, if the common `-Verbose` parameter is used, or both the parameters
3132
/// `-ArgumentList` and `-Property` are used, there won't be a simple type initializer
3233
/// available and the rule won't report any violation for the `New-Object` cmdlet.
34+
///
35+
/// Nevertheless, there are still some cases where the `New-Object` cmdlet might be
36+
/// replaceable with a type initializer that would be more efficient or has a better syntax,
37+
/// but an automatic correction can't be provided.
38+
/// For example if the `-ArgumentList` parameter is used with a variable,
39+
/// the rule will report a violation, but won't be able to provide a correction,
40+
/// as it's not possible to determine from the AST alone whether the variable contains a
41+
/// single value that can be used in a type initializer,
42+
/// or if it contains multiple values that would require splatting.
3343
/// </summary>
3444
public class AvoidUsingNewObject : ConfigurableRule
3545
{

Tests/Rules/AvoidUsingNewObject.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ BeforeDiscovery {
1212
$errors = $Null
1313
$null = [Parser]::ParseInput($ActualValue, [ref]$null, [ref]$errors)
1414
$succeeded = -not $errors -xor $Negate
15-
if ($succeeded) {
15+
if (-not $succeeded) {
1616
$not = if ($Negate) { ' not' }
17-
$failureMessage = "Expected '$ActualValue' to$Not parse$(if($Because) { " because $Because"})."
17+
$failureMessage = "Expected '$ActualValue'$Not to parse$(if($Because) { " because $Because"})."
1818
}
1919

2020
return [PSCustomObject]@{

docs/Rules/AvoidUsingNewObject.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ for ($i = 0; $i -lt 100000; $i++) {
5858
```
5959

6060
```powershell
61-
$hashSet = [System.Collections.Generic.HashSet[String]]([StringComparer]::InvariantCultureIgnoreCase)
61+
$hashSet = [System.Collections.Generic.HashSet[String]]::new([StringComparer]::InvariantCultureIgnoreCase)
6262
```
6363

6464
## Configuration
6565

6666
```powershell
6767
Rules = @{
68-
PSAvoidNewObject = @{
68+
PSAvoidUsingNewObject = @{
6969
Enable = $true
7070
}
7171
}

0 commit comments

Comments
 (0)