Skip to content

Commit f302df3

Browse files
committed
simplify patterns
1 parent d748c82 commit f302df3

File tree

9 files changed

+25
-25
lines changed

9 files changed

+25
-25
lines changed

ReflectionAnalyzers/Helpers/NameOf.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ member.ReflectedType is null ||
3636
return false;
3737
}
3838

39-
if (member.Symbol is { ContainingType: { IsAnonymousType: true } })
39+
if (member.Symbol is { ContainingType.IsAnonymousType: true })
4040
{
4141
if (member.TypeSource is InvocationExpressionSyntax getType &&
4242
getType.TryGetTarget(KnownSymbol.Object.GetType, context.SemanticModel, context.CancellationToken, out _) &&
@@ -86,7 +86,7 @@ member.Symbol is ITypeSymbol ||
8686

8787
internal static bool IsNameOf(ArgumentSyntax argument, [NotNullWhen(true)] out ExpressionSyntax? expression)
8888
{
89-
if (argument.Expression is InvocationExpressionSyntax { Expression: IdentifierNameSyntax { Identifier: { ValueText: "nameof" } }, ArgumentList: { Arguments: { Count: 1 } arguments } } &&
89+
if (argument.Expression is InvocationExpressionSyntax { Expression: IdentifierNameSyntax { Identifier.ValueText: "nameof" }, ArgumentList.Arguments: { Count: 1 } arguments } &&
9090
arguments.TrySingle(out var arg))
9191
{
9292
expression = arg.Expression;

ReflectionAnalyzers/Helpers/Reflection/Assembly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ internal static bool HasVisibleTypes(this IAssemblySymbol assembly)
2424
{
2525
return expression switch
2626
{
27-
MemberAccessExpressionSyntax { Name: IdentifierNameSyntax { Identifier: { ValueText: "GetType" } } } candidate
27+
MemberAccessExpressionSyntax { Name: IdentifierNameSyntax { Identifier.ValueText: "GetType" } } candidate
2828
=> Find(candidate.Expression, semanticModel, cancellationToken),
29-
MemberAccessExpressionSyntax { Name: IdentifierNameSyntax { Identifier: { ValueText: "Assembly" } } } candidate
29+
MemberAccessExpressionSyntax { Name: IdentifierNameSyntax { Identifier.ValueText: "Assembly" } } candidate
3030
when Type.TryGet(candidate.Expression, semanticModel, cancellationToken, out var typeInAssembly, out _)
3131
=> typeInAssembly.ContainingAssembly,
3232
_ => null,

ReflectionAnalyzers/Helpers/Reflection/MethodInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ when GetMethod.Match(candidate, semanticModel, cancellationToken) is { Member: {
2828

2929
InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax memberAccess } invocation
3030
when semanticModel.TryGetSymbol(invocation, KnownSymbol.PropertyInfo.GetGetMethod, cancellationToken, out _) &&
31-
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property: { GetMethod: { } getMethod } }
31+
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property.GetMethod: { } getMethod }
3232
=> new MethodInfo(reflectedType, getMethod),
3333

3434
InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax memberAccess } invocation
3535
when semanticModel.TryGetSymbol(invocation, KnownSymbol.PropertyInfo.GetSetMethod, cancellationToken, out _) &&
36-
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property: { SetMethod: { } setMethod } }
36+
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property.SetMethod: { } setMethod }
3737
=> new MethodInfo(reflectedType, setMethod),
3838

3939
MemberAccessExpressionSyntax memberAccess
4040
when semanticModel.TryGetSymbol(memberAccess, cancellationToken, out var symbol)
4141
=> symbol switch
4242
{
4343
{ } when symbol == KnownSymbol.PropertyInfo.GetMethod &&
44-
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property: { GetMethod: { } getMethod } }
44+
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property.GetMethod: { } getMethod }
4545
=> new MethodInfo(reflectedType, getMethod),
4646
{ } when symbol == KnownSymbol.PropertyInfo.SetMethod &&
47-
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property: { SetMethod: { } setMethod } }
47+
PropertyInfo.Find(memberAccess.Expression, semanticModel, cancellationToken) is { ReflectedType: { } reflectedType, Property.SetMethod: { } setMethod }
4848
=> new MethodInfo(reflectedType, setMethod),
4949
{ } when AssignedValue.FindSingle(symbol, semanticModel, cancellationToken) is { } assignedValue
5050
=> Find(assignedValue, semanticModel, cancellationToken),

ReflectionAnalyzers/Helpers/Reflection/Type.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,25 @@ private static bool TryGet(ExpressionSyntax expression, Recursion recursion, [No
160160
{
161161
switch (expression)
162162
{
163-
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name: { Identifier: { ValueText: "FieldType" } } } memberAccess
163+
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name.Identifier.ValueText: "FieldType" } memberAccess
164164
when GetField.Match(invocation, recursion.SemanticModel, recursion.CancellationToken) is { Single: { } single }:
165165
source = memberAccess;
166166
result = single.Type;
167167
return true;
168-
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name: { Identifier: { ValueText: "PropertyType" } } } memberAccess
168+
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name.Identifier.ValueText: "PropertyType" } memberAccess
169169
when GetProperty.Match(invocation, recursion.SemanticModel, recursion.CancellationToken) is { Single: { } single }:
170170
source = memberAccess;
171171
result = single.Type;
172172
return true;
173-
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name: { Identifier: { ValueText: "ReturnType" } } } memberAccess
173+
case MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation, Name.Identifier.ValueText: "ReturnType" } memberAccess
174174
when GetMethod.Match(invocation, recursion.SemanticModel, recursion.CancellationToken) is { Single: { } single }:
175175
source = memberAccess;
176176
result = single.ReturnType;
177177
return true;
178178
case TypeOfExpressionSyntax typeOf:
179179
source = typeOf;
180180
return recursion.SemanticModel.TryGetType(typeOf.Type, recursion.CancellationToken, out result);
181-
case InvocationExpressionSyntax { ArgumentList: { Arguments: { Count: 0 } } } invocation
181+
case InvocationExpressionSyntax { ArgumentList.Arguments.Count: 0 } invocation
182182
when invocation.TryGetMethodName(out var name) &&
183183
name == "GetType":
184184
switch (invocation.Expression)
@@ -187,7 +187,7 @@ when invocation.TryGetMethodName(out var name) &&
187187
source = invocation;
188188
if (recursion.SemanticModel.TryGetType(typeAccess.Expression, recursion.CancellationToken, out result))
189189
{
190-
if (result is INamedTypeSymbol { TypeArguments: { Length: 1 } typeArguments, ConstructedFrom: { SpecialType: SpecialType.System_Nullable_T } })
190+
if (result is INamedTypeSymbol { TypeArguments: { Length: 1 } typeArguments, ConstructedFrom.SpecialType: SpecialType.System_Nullable_T })
191191
{
192192
result = typeArguments[0];
193193
}
@@ -200,7 +200,7 @@ when invocation.TryGetMethodName(out var name) &&
200200
when expression.TryFirstAncestor(out TypeDeclarationSyntax? containingType):
201201
source = invocation;
202202
return recursion.SemanticModel.TryGetSymbol(containingType, recursion.CancellationToken, out result);
203-
case MemberBindingExpressionSyntax { Parent: { Parent: ConditionalAccessExpressionSyntax { Expression: { } } conditionalAccess } }:
203+
case MemberBindingExpressionSyntax { Parent.Parent: ConditionalAccessExpressionSyntax { Expression: { } } conditionalAccess }:
204204
source = invocation;
205205
return recursion.SemanticModel.TryGetType(conditionalAccess.Expression, recursion.CancellationToken, out result);
206206
}

ReflectionAnalyzers/NodeAnalzers/ActivatorAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static void Handle(SyntaxNodeAnalysisContext context)
7777
private static bool TryGetCreatedType(IMethodSymbol createInstance, InvocationExpressionSyntax invocation, SyntaxNodeAnalysisContext context, [NotNullWhen(true)] out ITypeSymbol? createdType, [NotNullWhen(true)] out ExpressionSyntax? typeSource)
7878
{
7979
if (createInstance.IsGenericMethod &&
80-
invocation.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax { TypeArgumentList: { Arguments: { Count: 1 } typeArguments } } } &&
80+
invocation.Expression is MemberAccessExpressionSyntax { Name: GenericNameSyntax { TypeArgumentList.Arguments: { Count: 1 } typeArguments } } &&
8181
typeArguments.TrySingle(out var typeArgument) &&
8282
context.SemanticModel.TryGetType(typeArgument, context.CancellationToken, out createdType))
8383
{
@@ -131,7 +131,7 @@ private static bool IsMissingDefaultConstructor(IMethodSymbol createInstance, In
131131
case LiteralExpressionSyntax literal when literal.IsKind(SyntaxKind.TrueLiteralExpression) &&
132132
Type.HasVisibleNonPublicMembers(createdType, recursive: false) &&
133133
!createdType.Constructors.TrySingle(
134-
x => x is { IsStatic: false, Parameters: { Length: 0 } },
134+
x => x is { IsStatic: false, Parameters.Length: 0 },
135135
out _):
136136
return true;
137137
case LiteralExpressionSyntax literal when literal.IsKind(SyntaxKind.FalseLiteralExpression) &&
@@ -145,7 +145,7 @@ private static bool IsMissingDefaultConstructor(IMethodSymbol createInstance, In
145145
bool HasDefaultConstructor()
146146
{
147147
return createdType.Constructors.TrySingle(
148-
x => x is { DeclaredAccessibility: Accessibility.Public, IsStatic: false, Parameters: { Length: 0 } },
148+
x => x is { DeclaredAccessibility: Accessibility.Public, IsStatic: false, Parameters.Length: 0 },
149149
out _);
150150
}
151151
}
@@ -180,7 +180,7 @@ private static bool TryGetValues(SeparatedSyntaxList<ArgumentSyntax> arguments,
180180
values = default;
181181
return false;
182182
case { } expression when semanticModel.TryGetType(expression, cancellationToken, out var type) &&
183-
type is IArrayTypeSymbol { ElementType: { SpecialType: SpecialType.System_Object } }:
183+
type is IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Object }:
184184
return Array.TryGetValues(expression, semanticModel, cancellationToken, out values);
185185
}
186186

ReflectionAnalyzers/NodeAnalzers/GetAccessorAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ private static void HandleInvocation(SyntaxNodeAnalysisContext context)
2929
context.Node is InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax propertyInfoAccess } invocation)
3030
{
3131
if (invocation.TryGetTarget(KnownSymbol.PropertyInfo.GetGetMethod, context.SemanticModel, context.CancellationToken, out _) &&
32-
PropertyInfo.Find(propertyInfoAccess.Expression, context.SemanticModel, context.CancellationToken) is { Property: { GetMethod: null } })
32+
PropertyInfo.Find(propertyInfoAccess.Expression, context.SemanticModel, context.CancellationToken) is { Property.GetMethod: null })
3333
{
3434
context.ReportDiagnostic(Diagnostic.Create(Descriptors.REFL003MemberDoesNotExist, invocation.GetNameLocation()));
3535
}
3636
else if (invocation.TryGetTarget(KnownSymbol.PropertyInfo.GetSetMethod, context.SemanticModel, context.CancellationToken, out _) &&
37-
PropertyInfo.Find(propertyInfoAccess.Expression, context.SemanticModel, context.CancellationToken) is { Property: { SetMethod: null } })
37+
PropertyInfo.Find(propertyInfoAccess.Expression, context.SemanticModel, context.CancellationToken) is { Property.SetMethod: null })
3838
{
3939
context.ReportDiagnostic(Diagnostic.Create(Descriptors.REFL003MemberDoesNotExist, invocation.GetNameLocation()));
4040
}
@@ -47,12 +47,12 @@ private static void HandleMemberAccess(SyntaxNodeAnalysisContext context)
4747
context.Node is MemberAccessExpressionSyntax { Expression: InvocationExpressionSyntax invocation } memberAccess)
4848
{
4949
if (IsProperty(memberAccess, KnownSymbol.PropertyInfo.GetMethod, context) &&
50-
PropertyInfo.Find(invocation, context.SemanticModel, context.CancellationToken) is { Property: { GetMethod: null } })
50+
PropertyInfo.Find(invocation, context.SemanticModel, context.CancellationToken) is { Property.GetMethod: null })
5151
{
5252
context.ReportDiagnostic(Diagnostic.Create(Descriptors.REFL003MemberDoesNotExist, memberAccess.Name.GetLocation()));
5353
}
5454
else if (IsProperty(memberAccess, KnownSymbol.PropertyInfo.SetMethod, context) &&
55-
PropertyInfo.Find(invocation, context.SemanticModel, context.CancellationToken) is { Property: { SetMethod: null } })
55+
PropertyInfo.Find(invocation, context.SemanticModel, context.CancellationToken) is { Property.SetMethod: null })
5656
{
5757
context.ReportDiagnostic(Diagnostic.Create(Descriptors.REFL003MemberDoesNotExist, memberAccess.Name.GetLocation()));
5858
}

ReflectionAnalyzers/NodeAnalzers/GetInterfaceAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ context.Node is InvocationExpressionSyntax invocation &&
5757
$"{match.ContainingNamespace}.{match.MetadataName}")));
5858
break;
5959
default:
60-
if (maybeNameSyntax is { HasValue: true, Value: { Identifier: { ValueText: "Name" } } })
60+
if (maybeNameSyntax is { HasValue: true, Value.Identifier.ValueText: "Name" })
6161
{
6262
context.ReportDiagnostic(
6363
Diagnostic.Create(

ReflectionAnalyzers/NodeAnalzers/GetXAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ private static bool UsesNameOfWrongMember(ReflectedMember member, Name name, Syn
525525

526526
private static bool IsPreferGetMemberThenAccessor(ReflectedMember member, Name name, Flags flags, Types types, SyntaxNodeAnalysisContext context, [NotNullWhen(true)] out string? callText)
527527
{
528-
if (member is { ReflectedType: { }, Invocation: { Expression: MemberAccessExpressionSyntax memberAccess } })
528+
if (member is { ReflectedType: { }, Invocation.Expression: MemberAccessExpressionSyntax memberAccess })
529529
{
530530
if (member is { Match: FilterMatch.Single, Symbol: IMethodSymbol method })
531531
{

ReflectionAnalyzers/NodeAnalzers/IsAssignableFromAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override void Initialize(AnalysisContext context)
2424

2525
internal static bool IsInstanceGetType(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken, [NotNullWhen(true)] out ExpressionSyntax? instance)
2626
{
27-
if (expression is InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax { Expression: { } temp, Name: { Identifier: { ValueText: "GetType" } } }, ArgumentList: { Arguments: { Count: 0 } } } invocation &&
27+
if (expression is InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax { Expression: { } temp, Name.Identifier.ValueText: "GetType" }, ArgumentList.Arguments.Count: 0 } invocation &&
2828
invocation.TryGetTarget(KnownSymbol.Object.GetType, semanticModel, cancellationToken, out _))
2929
{
3030
instance = temp;

0 commit comments

Comments
 (0)