From bdf8ef7de7e3e40f68006b9fec01838f2f7473e9 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Wed, 6 Nov 2024 14:37:44 +0800 Subject: [PATCH] HandleContentDialogShowAsyncAccess invocation --- .../UniversalAnalyzer.cs | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/UniversalAnalyzer.cs b/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/UniversalAnalyzer.cs index a0f9aeb..fdc9987 100644 --- a/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/UniversalAnalyzer.cs +++ b/src/Snap.Hutao.SourceGeneration/Snap.Hutao.SourceGeneration/UniversalAnalyzer.cs @@ -54,8 +54,7 @@ private static void CompilationStart(CompilationStartAnalysisContext context) context.RegisterSyntaxNodeAction(HandleEqualsAndNotEqualsExpressionShouldUsePatternMatching, SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression); context.RegisterSyntaxNodeAction(HandleIsPatternShouldUseRecursivePattern, SyntaxKind.IsPatternExpression); context.RegisterSyntaxNodeAction(HandleArgumentNullExceptionThrowIfNull, SyntaxKind.SuppressNullableWarningExpression); - - context.RegisterSyntaxNodeAction(HandleContentDialogShowAsyncAccess, SyntaxKind.SimpleMemberAccessExpression); + context.RegisterSyntaxNodeAction(HandleContentDialogShowAsyncAccess, SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.InvocationExpression); } private static void HandleTypeShouldBeInternal(SyntaxNodeAnalysisContext context) @@ -154,6 +153,12 @@ private static void HandleArgumentNullExceptionThrowIfNull(SyntaxNodeAnalysisCon { PostfixUnaryExpressionSyntax syntax = (PostfixUnaryExpressionSyntax)context.Node; + if (syntax.Kind() is not SyntaxKind.SuppressNullableWarningExpression) + { + return; + } + + // default! if (syntax.Operand is LiteralExpressionSyntax literal) { if (literal.IsKind(SyntaxKind.DefaultLiteralExpression)) @@ -162,12 +167,12 @@ private static void HandleArgumentNullExceptionThrowIfNull(SyntaxNodeAnalysisCon } } + // default(?)! if (syntax.Operand is DefaultExpressionSyntax) { return; } - Location location = syntax.GetLocation(); Diagnostic diagnostic = Diagnostic.Create(useArgumentNullExceptionThrowIfNullDescriptor, location); context.ReportDiagnostic(diagnostic); @@ -175,16 +180,42 @@ private static void HandleArgumentNullExceptionThrowIfNull(SyntaxNodeAnalysisCon private static void HandleContentDialogShowAsyncAccess(SyntaxNodeAnalysisContext context) { - MemberAccessExpressionSyntax syntax = (MemberAccessExpressionSyntax)context.Node; - if (syntax.Name.Identifier.Text == "ShowAsync") + switch (context.Node) { - ITypeSymbol? type = context.SemanticModel.GetTypeInfo(syntax.Expression).Type; - if (type?.ToDisplayString() == "Microsoft.UI.Xaml.Controls.ContentDialog") - { - Location location = syntax.GetLocation(); - Diagnostic diagnostic = Diagnostic.Create(avoidUsingContentDialogShowAsyncDescriptor, location); - context.ReportDiagnostic(diagnostic); - } + case MemberAccessExpressionSyntax memberAccess: + { + if (memberAccess.Name.Identifier.Text is "ShowAsync") + { + ISymbol? containingSymbol = context.SemanticModel.GetSymbolInfo(memberAccess.Name).Symbol?.ContainingSymbol; + + if (containingSymbol?.ToDisplayString() is "Microsoft.UI.Xaml.Controls.ContentDialog") + { + Location location = memberAccess.GetLocation(); + Diagnostic diagnostic = Diagnostic.Create(avoidUsingContentDialogShowAsyncDescriptor, location); + context.ReportDiagnostic(diagnostic); + } + } + } + + break; + case InvocationExpressionSyntax invocation: + { + if (invocation.Expression is IdentifierNameSyntax identifierName) + { + if (identifierName.Identifier.Text is "ShowAsync") + { + ISymbol? containingSymbol = context.SemanticModel.GetSymbolInfo(identifierName).Symbol?.ContainingSymbol; + + if (containingSymbol?.ToDisplayString() is "Microsoft.UI.Xaml.Controls.ContentDialog") + { + Location location = invocation.GetLocation(); + Diagnostic diagnostic = Diagnostic.Create(avoidUsingContentDialogShowAsyncDescriptor, location); + context.ReportDiagnostic(diagnostic); + } + } + } + } + break; } } } \ No newline at end of file