@@ -21,23 +21,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2121#endif
2222 public sealed class AvoidValidateRangeTypeMismatch : IScriptRule
2323 {
24- private static readonly HashSet < string > FloatingPointAndDecimalTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
24+ private static readonly HashSet < Type > FloatingPointAndDecimalTypes = new HashSet < Type >
2525 {
26- " double" , "System.Double" ,
27- "single" , " float" , "System.Single" ,
28- " decimal" , "System.Decimal"
26+ typeof ( double ) ,
27+ typeof ( float ) ,
28+ typeof ( decimal )
2929 } ;
3030
31- private static readonly HashSet < string > IntegralTypes = new HashSet < string > ( StringComparer . OrdinalIgnoreCase )
31+ private static readonly HashSet < Type > IntegralTypes = new HashSet < Type >
3232 {
33- " byte" , "System.Byte" ,
34- " sbyte" , "System.SByte" ,
35- " short" , "System.Int16" ,
36- " ushort" , "System.UInt16" ,
37- " int" , "System.Int32" ,
38- " uint" , "System.UInt32" ,
39- " long" , "System.Int64" ,
40- " ulong" , "System.UInt64"
33+ typeof ( byte ) ,
34+ typeof ( sbyte ) ,
35+ typeof ( short ) ,
36+ typeof ( ushort ) ,
37+ typeof ( int ) ,
38+ typeof ( uint ) ,
39+ typeof ( long ) ,
40+ typeof ( ulong )
4141 } ;
4242
4343 public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName )
@@ -51,7 +51,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5151 foreach ( ParameterAst parameterAst in parameterAsts )
5252 {
5353 TypeConstraintAst typeConstraint = parameterAst . Attributes . OfType < TypeConstraintAst > ( ) . FirstOrDefault ( ) ;
54- if ( typeConstraint == null || ! FloatingPointAndDecimalTypes . Contains ( typeConstraint . TypeName . FullName ) )
54+ if ( typeConstraint == null )
55+ {
56+ continue ;
57+ }
58+
59+ Type parameterType = typeConstraint . TypeName . GetReflectionType ( ) ;
60+ if ( parameterType == null )
61+ {
62+ continue ;
63+ }
64+
65+ if ( parameterType . IsArray )
66+ {
67+ parameterType = parameterType . GetElementType ( ) ;
68+ }
69+
70+ if ( ! FloatingPointAndDecimalTypes . Contains ( parameterType ) )
5571 {
5672 continue ;
5773 }
@@ -60,7 +76,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6076 {
6177 if ( ! IsValidateRangeAttribute ( attributeAst )
6278 || attributeAst . PositionalArguments . Count < 2
63- || ! attributeAst . PositionalArguments . Take ( 2 ) . Any ( IsIntegralBoundary ) )
79+ || ! attributeAst . PositionalArguments . Take ( 2 ) . All ( IsIntegralBoundary ) )
6480 {
6581 continue ;
6682 }
@@ -79,14 +95,12 @@ private static bool IsIntegralBoundary(ExpressionAst expressionAst)
7995 {
8096 if ( expressionAst is ConstantExpressionAst constantExpressionAst )
8197 {
82- return constantExpressionAst . Value != null
83- && IntegralTypes . Contains ( constantExpressionAst . Value . GetType ( ) . FullName ) ;
98+ return constantExpressionAst . Value != null && IntegralTypes . Contains ( constantExpressionAst . Value . GetType ( ) ) ;
8499 }
85100
86101 if ( expressionAst is ConvertExpressionAst convertExpressionAst )
87102 {
88- return IntegralTypes . Contains ( convertExpressionAst . Type . TypeName . FullName )
89- && IsIntegralBoundary ( convertExpressionAst . Child ) ;
103+ return IntegralTypes . Contains ( convertExpressionAst . Type . TypeName . GetReflectionType ( ) ) ;
90104 }
91105
92106 if ( expressionAst is UnaryExpressionAst unaryExpressionAst
0 commit comments