Skip to content

CA1849 is not raised by GetAwaiter().GetResult() #7811

@jloscos

Description

@jloscos

Diagnostic ID : CA1849

Description

According to the documentation the CA1849 :

All methods where an Async-suffixed equivalent exists will produce this warning when called from a Task-returning method. In addition, calling Task.Wait(), Task.Result, or Task.GetAwaiter().GetResult() will produce this warning.

The warning is triggered as expected when calling .Wait() or .Result but not when calling .GetAwaiter().GetResult().

Steps to reproduce

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisLevel>latest</AnalysisLevel>
    <AnalysisMode>All</AnalysisMode>    
  </PropertyGroup>
</Project>
public static async Task<string> ReadFileAsync()
{
    // The following line does not trigger the CA1849 rule
    var content = File.ReadAllTextAsync("input.txt").GetAwaiter().GetResult();
    return content;
}

Expected Behavior

CA1849 is raised

Actual Behavior

No warning are raised

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions