How to configure resharper to warn about asynchronous methods returning void

I had some uncomfortable intermittent errors in which the async methods returned invalid rather than Task, and therefore they were not expected. in order to avoid such problems in the future, I am trying to create a custom template to highlight methods such as warnings.

I created a template, for example:

async void $methodname$($arguments$) { $statements$ } 

(where the arguments and statements allow an unlimited number of matches of each and the method - this is an identifier containing 1 or more characters of a legal identifier)

Oddly enough, this emphasizes all void methods, not just async void methods. Can anyone see my mistake? Since this looks like a generic template that is trying to match, and Google is not reporting this, I doubt that this is an unresolved issue in Reshaper 8.2.1.

PS I know that my template will not conform to general methods - if I can make it work for non-general methods, I will fix this later)

UPDATE

As noted in the comments, I understand that there are a number of false positives and false negatives that this rule did not handle. My concern is how to get asynchronous matching for working in resharper - as soon as this works, I can look to make the rule more precise.

+6
source share
2 answers

You cannot do this. The way Resharper custom templates work does not seem to understand the async keyword yet (therefore, it highlights all void return methods, not just async tags). You can also see this example that matches async and not async methods.

What you can do is try to find async methods by looking for method names ending in "Async", for example:

 async void $method$($parameters$) { $statements$ } 

If the method identifier is determined by this regular expression: \b\w+(Async)\b

This will allow you to replace this:

 void RunAsync() // or async void RunAsync() { ... } 

Wherein:

 async Task RunAsync() { ... } 
+4
source

We use the free ReCommended Extension for ReSharper in our projects to analyze asynchronous errors. The extension emphasizes the inappropriate use of asynchronous errors and provides obvious quick fixes.

The analysis rules are described here: ReCommended-Extension wiki .

+4
source

Source: https://habr.com/ru/post/972189/


All Articles