Skip to main content

CRR0030 - Redundant 'await'

  • 2 minutes to read

The Static Analysis tool detects whether async/await keywords are redundant.

You can omit async/await keywords if a method does not have the continuation code block. This allows you to avoid unnecessary delay in code execution. For example, you can return a Task from a method instead of the task’s result if it is a Task type method.

async Task<string> DemoMethod(bool value, CancellationToken token) {
    if (value)
        return await Task.Run(() => "result1", token).ConfigureAwait(false);  // CRR0030
    return await Task.Run(() => "result2", token).ConfigureAwait(false);  // CRR0030
}

Change the code above as follows to omit async/await:

Task<string> DemoMethod(bool value, CancellationToken token) {
    if (value)
        return Task.Run(() => "result1", token);
    return Task.Run(() => "result2", token);
}

If your async method contains the continuation code you cannot omit async/await keywords:

async Task<string> DemoMethodAsync(bool value, CancellationToken token) {
    if (value)
        return await SomeMethodWhichReturnedTask().ConfigureAwait(false);

    var someValue = await SomeAsyncMethod().ConfigureAwait(false);
    if (someValue)
        DoSomething();

    return await AnotherMethodWhichReturnedTask().ConfigureAwait(false);
}

In the code above, when thread reaches await SomeAsyncMethod() this await makes the compiler to run operation on a new task and waits while this task is completed. Then, await causes thread to return and continue with execution. After the compiler finishes SomeAsyncMethod() it executes the DoSomething() method.