Skip to content

Commit

Permalink
HandleContentDialogShowAsyncAccess invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Nov 6, 2024
1 parent ba28226 commit bdf8ef7
Showing 1 changed file with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -162,29 +167,55 @@ 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);
}

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;
}
}
}

0 comments on commit bdf8ef7

Please sign in to comment.