@@ -19,11 +19,11 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
1919#endif
2020
2121 /// <summary>
22- /// Rule that reports an warning when the New-Object cmdlet is used in a script.
22+ /// Rule that reports a warning when the New-Object cmdlet is used in a script.
2323 /// The rule implements a correction that suggests using type-casting or type constructor.
2424 ///
2525 /// Note:
26- /// In most cases if there isn't an automatic correction isn't available,
26+ /// In most cases if there isn't an automatic correction available,
2727 /// the rule won't report any violation either.
2828 /// This is because if there isn't an automatic correction available, it generally means
2929 /// that there isn't a simple type-casting or type constructor that can be used that would
@@ -63,8 +63,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6363
6464 IEnumerable < CommandAst > newObjectAsts = ast . FindAll ( testAst =>
6565 testAst is CommandAst cmdAst &&
66- cmdAst . GetCommandName ( ) != null &&
67- cmdAst . GetCommandName ( ) . Equals ( "New-Object" , StringComparison . OrdinalIgnoreCase ) ,
66+ ( cmdAst . GetCommandName ( ) as string ) is string commandName &&
67+ commandName . Equals ( "New-Object" , StringComparison . OrdinalIgnoreCase ) ,
6868 true
6969 ) . Cast < CommandAst > ( ) ;
7070
@@ -77,19 +77,24 @@ testAst is CommandAst cmdAst &&
7777 // But not both, as that would mean there isn't a simple
7878 // type initializer available as a replacement.
7979 if (
80- bindingResult . BoundParameters . Count = = 2 &&
80+ bindingResult . BoundParameters . Count < = 2 &&
8181 bindingResult . BoundParameters . TryGetValue ( "TypeName" , out ParameterBindingResult asTypeName ) &&
8282 asTypeName . ConstantValue is string typeName
8383 ) {
8484 Boolean isProperty = bindingResult . BoundParameters . TryGetValue ( "Property" , out ParameterBindingResult boundResult ) ;
8585 if ( ! isProperty )
8686 {
8787 Boolean isArgument = bindingResult . BoundParameters . TryGetValue ( "ArgumentList" , out ParameterBindingResult argumentResult ) ;
88- if ( isArgument ) { boundResult = argumentResult ; } else { continue ; }
88+ if ( isArgument ) { boundResult = argumentResult ; }
8989 }
9090
9191 string correction = null ;
92- if ( isProperty )
92+ if ( boundResult == null )
93+ {
94+ // No `-Property` or `-ArgumentList` parameter was used, so we suggest a parameterless constructor call.
95+ correction = "[" + typeName + "]::new()" ;
96+ }
97+ else if ( isProperty )
9398 {
9499 correction = "[" + typeName + "]" + boundResult . Value . Extent . Text ;
95100 }
0 commit comments