IDISP011
April 2, 2023 ยท View on GitHub
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...")]