Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is CS4014 not shown for all functions that return a task?

Tags:

c#

async-await

Suppose the following code:

private async Task Test1Async() => await Task.Delay(1000).ConfigureAwait(false);
private Task Test2Async() => Test1Async();

Functionally, these functions are exactly the same but the compiler treats calling these methods different. The following code compiles, but issues a CS4014 warning:

private void Test() => Test1Async();   // CS4014 is shown

It generates the warning "because this call is not awaited, the current method continues to run before the call is completed". This is a proper warning, because it often indicates a flaw in your code. In case you actually want this behavior, then you can solve it by using the following code:

private void Test() => _ = Test1Async();   // CS4014 is not shown anymore

Assigning the value to _ is a relative new feature to indicate that the value is ignored intentionally.

This code doesn't raise CS4014:

private void Test() => Test2Async();   // CS4014 is not shown!

Of course, I could rewrite all my methods to use the async/await method, but this results in more code that runs less efficient (due to the state machine generated by the async keyword). Maybe I will never forget about it, but my coworkers might and then I won't get a trigger (or I call a third-party library that doesn't use async).

There is also a difference in the warning about the returned Task usage.

Does anyone know why this warning is not generated for methods that return a Task that don't use the async keyword?

like image 349
Ramon de Klein Avatar asked Aug 10 '17 12:08

Ramon de Klein


People also ask

Is async but does not return a task?

Async methods that don't contain a return statement or that contain a return statement that doesn't return an operand usually have a return type of Task. Such methods return void if they run synchronously.

What does an async task return?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

When an asynchronous method is executed the code runs but nothing happens other than a compiler warning?

When a asynchronous method is executed, the code runs but nothing happens other than a compiler warning. What is most likely causing the method to not return anything? (A) The return yield statement is missing at the end of the method.

How do you call async method without await?

@Servy async creates a state machine that manages any awaits within the async method. If there are no await s within the method it still creates that state machine--but the method is not asynchronous. And if the async method returns void , there's nothing to await. So, it's more than just awaiting a result.


1 Answers

Does anyone know why this warning is not generated for methods that return a Task that don't use the async keyword?

Probably to avoid spurious warnings on legacy code. Task predated async, and there's a number of methods out there that return Task but don't have anything to do with asynchronous code.

Personally, I think ignoring a Task return value is almost certainly a mistake, even in non-asynchronous code. But I assume that the MS team ran metrics and determined this warning would be too noisy for those kinds of code bases.

like image 162
Stephen Cleary Avatar answered Oct 06 '22 00:10

Stephen Cleary