Skip to content

Commit

Permalink
Fix #16: AsyncVoid ignores EventHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
semihokur committed May 11, 2022
1 parent 8bba5a6 commit ecdac26
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
30 changes: 28 additions & 2 deletions AsyncFixer.Test/AsyncVoidTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Microsoft.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using AsyncFixer.AsyncVoid;
using Xunit;
using AsyncFixer.AsyncVoid;
using Verify = AsyncFixer.Test.Helpers.CSharpCodeFixVerifier<
AsyncFixer.AsyncVoid.AsyncVoidAnalyzer,
AsyncFixer.AsyncVoid.AsyncVoidFixer>;

namespace AsyncFixer.Test
{
Expand Down Expand Up @@ -120,6 +123,29 @@ private static Task ProcessAsync()
VerifyCSharpFix(test, test);
}

[Fact]
public Task NoWarn_EventHandler()
{
//No diagnostics expected to show up

var test = @"
using System;
using System.Threading.Tasks;
class Program
{
public void foo()
{
AppDomain.CurrentDomain.ProcessExit += async (a, b) =>
{
await Task.Delay(100);
};
}
}";

return Verify.VerifyAsync(test);
}

protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new AsyncVoidFixer();
Expand Down
6 changes: 6 additions & 0 deletions AsyncFixer/AsyncVoid/AsyncVoidAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ private void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
private void AnalyzeAnonymousFunction(OperationAnalysisContext context)
{
var operation = (IAnonymousFunctionOperation)context.Operation;

if (operation.IsEventHandler())
{
return;
}

var symbol = operation.Symbol;
if (symbol == null || !symbol.IsAsync || !symbol.ReturnsVoid)
{
Expand Down
16 changes: 16 additions & 0 deletions AsyncFixer/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;

namespace AsyncFixer
{
Expand Down Expand Up @@ -78,6 +79,21 @@ public static ExpressionSyntax RemoveConfigureAwait(ExpressionSyntax expression)
return expression;
}

public static bool IsEventHandler(this IOperation operation)
{
while (operation != null)
{
if (operation is IEventAssignmentOperation)
{
return true;
}

operation = operation.Parent;
}

return false;
}

public static T FirstAncestorOrSelfUnderGivenNode<T>(this SyntaxNode node, SyntaxNode parent)
where T : SyntaxNode
{
Expand Down

0 comments on commit ecdac26

Please sign in to comment.