Back to Devexpress

CRR0030 - Redundant 'await'

coderushforroslyn-119687-static-code-analysis-analyzers-library-crr0030-redundant-await.md

latest3.0 KB
Original Source

CRR0030 - Redundant 'await'

  • Feb 28, 2025
  • 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.

csharp
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
}
vb
Async Function DemoMethod(value As Boolean, token As CancellationToken) As Task(Of String)
    If value Then
        Return Await Task.Run(Function() "result1", token).ConfigureAwait(False) ' CRR0030
    End If
    Return Await Task.Run(Function() "result2", token).ConfigureAwait(False) ' CRR0030
End Function

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

csharp
Task<string> DemoMethod(bool value, CancellationToken token) {
    if (value)
        return Task.Run(() => "result1", token);
    return Task.Run(() => "result2", token);
}
vb
Private Function DemoMethod(value As Boolean, token As CancellationToken) As Task(Of String)
    If value Then
        Return Task.Run(Function() "result1", token)
    End If
    Return Task.Run(Function() "result2", token)
End Function

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

csharp
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);
}
vb
Private Async Function DemoMethodAsync(ByVal value As Boolean, ByVal token As CancellationToken) As Task(Of String)
    If value Then Return Await SomeMethodWhichReturnedTask().ConfigureAwait(False)
    Dim someValue = Await SomeAsyncMethod().ConfigureAwait(False)
    If someValue Then DoSomething()
    Return Await AnotherMethodWhichReturnedTask().ConfigureAwait(False)
End Function

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.

See Also

Suppress Analyzers