Open
Description
This sounds actually an easy one to implement but it can easily turn out to be a challenging one. Let me put it here and we can discuss the corner cases.
We should make sure to directly return the Task
or Task<T>
object without await
ing inside a method where you don't need to run any continuation. For instance:
public class Foo
{
private const string FooBar = "foobar";
private readonly IBar _bar;
public Foo(IBar bar)
{
_bar = bar;
}
public async Task<Bar> GetBarsAsync()
{
Bar bar = await _bar.GetBarAsync(FooBar);
return bar;
}
}
The GetBarsAsync
method here doesn't need to await on the _bar.GetBarAsync
method call. It can just directly return the result as below:
public Task<Bar> GetBarsAsync()
{
return _bar.GetBarAsync(FooBar);
}
Cases to Cover
- IMO, This should be an information level diagnostic.
- Cases where awaiting on
ConfigureAwait(false)
needs to be covered and it should be omitted on the modified code.