11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4- using System ;
54using System . Collections . Immutable ;
65using System . Linq ;
76using Microsoft . AspNetCore . Analyzers . RouteEmbeddedLanguage . Infrastructure ;
@@ -22,13 +21,8 @@ public static bool IsEnumerable(this ITypeSymbol type, INamedTypeSymbol enumerab
2221 return type . ImplementsInterface ( enumerable ) || SymbolEqualityComparer . Default . Equals ( type , enumerable ) ;
2322 }
2423
25- public static bool ImplementsValidationAttribute ( this ITypeSymbol typeSymbol , INamedTypeSymbol ? validationAttributeSymbol )
24+ public static bool ImplementsValidationAttribute ( this ITypeSymbol typeSymbol , INamedTypeSymbol validationAttributeSymbol )
2625 {
27- if ( validationAttributeSymbol is null )
28- {
29- return false ;
30- }
31-
3226 var baseType = typeSymbol . BaseType ;
3327 while ( baseType != null )
3428 {
@@ -42,9 +36,8 @@ public static bool ImplementsValidationAttribute(this ITypeSymbol typeSymbol, IN
4236 return false ;
4337 }
4438
45- public static ITypeSymbol UnwrapType ( this ITypeSymbol type , INamedTypeSymbol ? enumerable )
39+ public static ITypeSymbol UnwrapType ( this ITypeSymbol type , INamedTypeSymbol enumerable )
4640 {
47-
4841 if ( type . OriginalDefinition . SpecialType == SpecialType . System_Nullable_T &&
4942 type is INamedTypeSymbol { TypeArguments . Length : 1 } )
5043 {
@@ -59,11 +52,6 @@ public static ITypeSymbol UnwrapType(this ITypeSymbol type, INamedTypeSymbol? en
5952 type = type . WithNullableAnnotation ( NullableAnnotation . NotAnnotated ) ;
6053 }
6154
62- if ( enumerable is null )
63- {
64- return type ;
65- }
66-
6755 if ( type is INamedTypeSymbol namedType && namedType . IsEnumerable ( enumerable ) && namedType . TypeArguments . Length == 1 )
6856 {
6957 // Extract the T from an IEnumerable<T> or List<T>
@@ -73,13 +61,8 @@ public static ITypeSymbol UnwrapType(this ITypeSymbol type, INamedTypeSymbol? en
7361 return type ;
7462 }
7563
76- internal static bool ImplementsInterface ( this ITypeSymbol type , ITypeSymbol ? interfaceType )
64+ internal static bool ImplementsInterface ( this ITypeSymbol type , ITypeSymbol interfaceType )
7765 {
78- if ( interfaceType is null )
79- {
80- return false ;
81- }
82-
8366 foreach ( var iface in type . AllInterfaces )
8467 {
8568 if ( SymbolEqualityComparer . Default . Equals ( interfaceType , iface ) )
@@ -90,13 +73,8 @@ internal static bool ImplementsInterface(this ITypeSymbol type, ITypeSymbol? int
9073 return false ;
9174 }
9275
93- internal static ImmutableArray < INamedTypeSymbol > ? GetJsonDerivedTypes ( this ITypeSymbol type , INamedTypeSymbol ? jsonDerivedTypeAttribute )
76+ internal static ImmutableArray < INamedTypeSymbol > ? GetJsonDerivedTypes ( this ITypeSymbol type , INamedTypeSymbol jsonDerivedTypeAttribute )
9477 {
95- if ( jsonDerivedTypeAttribute is null )
96- {
97- return null ;
98- }
99-
10078 var derivedTypes = ImmutableArray . CreateBuilder < INamedTypeSymbol > ( ) ;
10179 foreach ( var attribute in type . GetAttributes ( ) )
10280 {
@@ -117,9 +95,7 @@ internal static bool ImplementsInterface(this ITypeSymbol type, ITypeSymbol? int
11795 // types themselves so we short-circuit on them.
11896 internal static bool IsExemptType ( this ITypeSymbol type , WellKnownTypes wellKnownTypes )
11997 {
120- try
121- {
122- return SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . Microsoft_AspNetCore_Http_HttpContext ) )
98+ return SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . Microsoft_AspNetCore_Http_HttpContext ) )
12399 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . Microsoft_AspNetCore_Http_HttpRequest ) )
124100 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . Microsoft_AspNetCore_Http_HttpResponse ) )
125101 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . System_Threading_CancellationToken ) )
@@ -128,12 +104,6 @@ internal static bool IsExemptType(this ITypeSymbol type, WellKnownTypes wellKnow
128104 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . Microsoft_AspNetCore_Http_IFormFile ) )
129105 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . System_IO_Stream ) )
130106 || SymbolEqualityComparer . Default . Equals ( type , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . System_IO_Pipelines_PipeReader ) ) ;
131-
132- }
133- catch ( InvalidOperationException )
134- {
135- return false ;
136- }
137107 }
138108
139109 internal static IPropertySymbol ? FindPropertyIncludingBaseTypes ( this INamedTypeSymbol typeSymbol , string propertyName )
@@ -162,13 +132,8 @@ internal static bool IsExemptType(this ITypeSymbol type, WellKnownTypes wellKnow
162132 /// <param name="parameter">The parameter to check.</param>
163133 /// <param name="fromServiceMetadataSymbol">The symbol representing the [FromService] attribute.</param>
164134 /// <param name="fromKeyedServiceAttributeSymbol">The symbol representing the [FromKeyedService] attribute.</param>
165- internal static bool IsServiceParameter ( this IParameterSymbol parameter , INamedTypeSymbol ? fromServiceMetadataSymbol , INamedTypeSymbol ? fromKeyedServiceAttributeSymbol )
135+ internal static bool IsServiceParameter ( this IParameterSymbol parameter , INamedTypeSymbol fromServiceMetadataSymbol , INamedTypeSymbol fromKeyedServiceAttributeSymbol )
166136 {
167- if ( fromServiceMetadataSymbol is null || fromKeyedServiceAttributeSymbol is null )
168- {
169- return false ;
170- }
171-
172137 return parameter . GetAttributes ( ) . Any ( attr =>
173138 attr . AttributeClass is not null &&
174139 ( attr . AttributeClass . ImplementsInterface ( fromServiceMetadataSymbol ) ||
@@ -181,13 +146,8 @@ attr.AttributeClass is not null &&
181146 /// <param name="property">The property to check.</param>
182147 /// <param name="fromServiceMetadataSymbol">The symbol representing the [FromServices] attribute.</param>
183148 /// <param name="fromKeyedServiceAttributeSymbol">The symbol representing the [FromKeyedServices] attribute.</param>
184- internal static bool IsServiceProperty ( this IPropertySymbol property , INamedTypeSymbol ? fromServiceMetadataSymbol , INamedTypeSymbol ? fromKeyedServiceAttributeSymbol )
149+ internal static bool IsServiceProperty ( this IPropertySymbol property , INamedTypeSymbol fromServiceMetadataSymbol , INamedTypeSymbol fromKeyedServiceAttributeSymbol )
185150 {
186- if ( fromServiceMetadataSymbol is null || fromKeyedServiceAttributeSymbol is null )
187- {
188- return false ;
189- }
190-
191151 return property . GetAttributes ( ) . Any ( attr =>
192152 attr . AttributeClass is not null &&
193153 ( attr . AttributeClass . ImplementsInterface ( fromServiceMetadataSymbol ) ||
@@ -199,29 +159,19 @@ attr.AttributeClass is not null &&
199159 /// </summary>
200160 /// <param name="property">The property to check.</param>
201161 /// <param name="jsonIgnoreAttributeSymbol">The symbol representing the [JsonIgnore] attribute.</param>
202- internal static bool IsJsonIgnoredProperty ( this IPropertySymbol property , INamedTypeSymbol ? jsonIgnoreAttributeSymbol )
162+ internal static bool IsJsonIgnoredProperty ( this IPropertySymbol property , INamedTypeSymbol jsonIgnoreAttributeSymbol )
203163 {
204- if ( jsonIgnoreAttributeSymbol is null )
205- {
206- return false ;
207- }
208-
209164 return property . GetAttributes ( ) . Any ( attr =>
210165 attr . AttributeClass is not null &&
211166 SymbolEqualityComparer . Default . Equals ( attr . AttributeClass , jsonIgnoreAttributeSymbol ) ) ;
212167 }
213168
214- internal static bool IsSkippedValidationProperty ( this IPropertySymbol property , INamedTypeSymbol ? skipValidationAttributeSymbol )
169+ internal static bool IsSkippedValidationProperty ( this IPropertySymbol property , INamedTypeSymbol skipValidationAttributeSymbol )
215170 {
216- if ( skipValidationAttributeSymbol is null )
217- {
218- return false ;
219- }
220-
221171 return property . HasAttribute ( skipValidationAttributeSymbol ) || property . Type . HasAttribute ( skipValidationAttributeSymbol ) ;
222172 }
223173
224- internal static bool IsSkippedValidationParameter ( this IParameterSymbol parameter , INamedTypeSymbol ? skipValidationAttributeSymbol )
174+ internal static bool IsSkippedValidationParameter ( this IParameterSymbol parameter , INamedTypeSymbol skipValidationAttributeSymbol )
225175 {
226176 return parameter . HasAttribute ( skipValidationAttributeSymbol ) || parameter . Type . HasAttribute ( skipValidationAttributeSymbol ) ;
227177 }
0 commit comments