Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 1.56 KB

IDISP011.md

File metadata and controls

65 lines (49 loc) · 1.56 KB

IDISP011

Don't return disposed instance

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

Description

Don't return disposed instance.

Motivation

In the below example the FileStream is disposed and not usable

public FileStream M(string fileName)
{
    using var stream = File.OpenRead(fileName);
    return stream;
}

How to fix violations

Don't dispose an instance that is returned. Caller is responsible for disposing.

public FileStream M(string fileName)
{
    return File.OpenRead(fileName);
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable IDISP011 // Don't return disposed instance
Code violating the rule here
#pragma warning restore IDISP011 // Don't return disposed instance

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

#pragma warning disable IDISP011 // Don't return disposed instance

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP011:Don't return disposed instance", 
    Justification = "Reason...")]