I've experienced some awkward intermittent bugs where async methods were returning void rather than Task and hence were not being awaited. to avoid such problems in the future I'm trying to create a custom resharper pattern to highlight such methods as warnings.
I've created a pattern like:
async void $methodname$($arguments$)
{
$statements$
}
(where arguments and statements allow unlimited number of each and method matches is an identifier containing 1 or more legal identifier characters)
Oddly this highlights all void methods rather than just async void methods. Can anyone see my mistake? As it seems like a common pattern to attempt to match, and google doesn't report it, I doubt if it's an outstanding issue in Reshaper 8.2.1.
P.S. I know my pattern won't match generic methods - if I can get it working for non-generic ones I'll fix that up later)
UPDATE
As highlighted in the comments I realise there are a number of false positives and false negatives that this rule would not handle. My concern here is how to get async matching to work in resharper - once that works I can look to make the rule more precise.
You can't really do that. The way Resharper's custom patterns work evidently doesn't yet understand the async
keyword (that's why it highlights all void-returning methods, and not just the async
ones). You can also see this example, which matches both async
and non-async
methods.
What you can do is try to find async
methods by searching for method names ending with "Async", like that:
async void $method$($parameters$)
{
$statements$
}
Where the method identifier is defined by this regexp: \b\w+(Async)\b
This will allow you to replace this:
void RunAsync() // or async void RunAsync()
{
...
}
With this:
async Task RunAsync()
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With