Skip to content

Replace Task.Result with await Task #734

@AmadeusW

Description

@AmadeusW

New analyzer: Replace Task.Result with await and make method async Task

Recently we've found some deadlocks in our code. They were caused by a stray .Result in situations when there was an async method earlier in the call stack. Using .Result is considered a bad practice.
I propose an analyzer that removes .Result and replaces it with an await call

Before:

private async Task<bool> DoSomethingAsync()
{
    return AsyncMethod().Result;
}

After:

private async Task<bool> DoSomethingAsync()
{
    return await AsyncMethod();
}

Category: Design
Severity: Warning
Diagnostic id: CC0122

I'm curious what's your opinion on

  1. This refactoring will cause a build error, because method's signature changes
  2. Are there any use cases where using .Result is OK?

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