Skip to content

Avoid awaiting inside a method where you don't need to run any continuation #24

Open
@tugberkugurlu

Description

@tugberkugurlu

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 awaiting 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions