IDISP005
November 25, 2021 ยท View on GitHub
Return type should indicate that the value should be disposed
| Topic | Value |
|---|---|
| Id | IDISP005 |
| Severity | Warning |
| Enabled | True |
| Category | IDisposableAnalyzers.Correctness |
| Code | ReturnValueAnalyzer |
Description
Return type should indicate that the value should be disposed.
Motivation
In the following method an IDisposable is created and returned but the api is not clear about that the caller should dispose the recieved value.
public object Meh()
{
return File.OpenRead(string.Empty);
}
How to fix violations
Use a returntype that is or implements IDisposable
public IDisposable Meh()
{
return File.OpenRead(string.Empty);
}
or
public Stream Meh()
{
return File.OpenRead(string.Empty);
}
Configure severity
Via ruleset file.
Configure the severity per project, for more info see MSDN.
Via #pragma directive.
#pragma warning disable IDISP005 // Return type should indicate that the value should be disposed
Code violating the rule here
#pragma warning restore IDISP005 // Return type should indicate that the value should be disposed
Or put this at the top of the file to disable all instances.
#pragma warning disable IDISP005 // Return type should indicate that the value should be disposed
Via attribute [SuppressMessage].
[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness",
"IDISP005:Return type should indicate that the value should be disposed",
Justification = "Reason...")]