Skip to content

Latest commit

 

History

History
66 lines (50 loc) · 1.59 KB

IDISP013.md

File metadata and controls

66 lines (50 loc) · 1.59 KB

IDISP013

Await in using

Topic Value
Id IDISP013
Severity Warning
Enabled True
Category IDisposableAnalyzers.Correctness
Code ReturnValueAnalyzer

Description

Await in using.

Motivation

public Task<string> M(string url)
{
    using var client = new Client();
    return client.GetStringAsync(url);
}

In the above code the client is disposed when the method returns which may be before the task completes and depending on implementation this can lead to ObjectDisposedException

How to fix violations

Use the fix to change the code to:

public async Task<string> M(string url)
{
    using var client = new Client();
    return await client.GetStringAsync(url);
}

Configure severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#pragma warning disable IDISP013 // Await in using
Code violating the rule here
#pragma warning restore IDISP013 // Await in using

Or put this at the top of the file to disable all instances.

#pragma warning disable IDISP013 // Await in using

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP013:Await in using", 
    Justification = "Reason...")]