Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

AK1003 - Add ReceiveAsync with sync lambda analyzer and code fix #61

Merged

Conversation

Arkatufus
Copy link
Contributor

@Arkatufus Arkatufus commented Jan 23, 2024

Partially fixes #57

Changes

Add analyzer and code fix for ReceiveAsync<T>() and ReceiveAnyAsync() invocations that does not use async lambda expression.

Notes

This implementation does not analyze ReceiveAsync<T>() and ReceiveAnyAsync() that uses a method delegate. This will need a separate analyzer.

Example:

public sealed class MyActor : ReceiveActor
{
    public MyActor()
    {
        ReceiveAsync<string>(Handler); // This can't be analyzed, for now.
    }
    
    private async Task Handler(string s) { }
}

Code fixes

ReceiveAsync with async keyword and no await keywords

ReceiveAsync<string>(async str =>
{
    Sender.Tell(str);
});

to

Receive<string>(str =>
{
    Sender.Tell(str);
});

ReceiveAnyAsync with async keyword and no await keywords

ReceiveAnyAsync(async o =>
{
    Sender.Tell(o);
});

to

ReceiveAny(o =>
{
    Sender.Tell(o);
});

ReceiveAsync with no body block with async keyword and no await keywords

ReceiveAsync<string>(async str => Sender.Tell(str));

to

Receive<string>(str => Sender.Tell(str));

ReceiveAnyAsync with no body block with async keyword and no await keywords

ReceiveAnyAsync(async o => Sender.Tell(o));

to

ReceiveAny(o => Sender.Tell(o));

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left one question for you @Arkatufus

}

// second case, lambda expression contains a block and one of its child is awaited, OK
if(lambdaExpr.Body.DescendantNodes().OfType<AwaitExpressionSyntax>().Any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Aaronontheweb Aaronontheweb merged commit f14ac23 into akkadotnet:dev Jan 25, 2024
2 checks passed
@Arkatufus Arkatufus deleted the AK1003-ReceiveAsync-without-asyc branch January 25, 2024 18:27
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AK1000: warn users about using ReceiveAsync when not actually doing any await-ing
2 participants