IDISP004
November 25, 2021 ยท View on GitHub
Don't ignore created IDisposable
| Topic | Value |
|---|---|
| Id | IDISP004 |
| Severity | Warning |
| Enabled | True |
| Category | IDisposableAnalyzers.Correctness |
| Code | CreationAnalyzer |
Description
Don't ignore created IDisposable.
Motivation
In the following code the file is opened but not closed.
public sealed class Foo
{
public Foo()
{
File.OpenRead("file.txt");
}
}
How to fix violations
Assign the value to a field or property or use it in a using if it is a temporary value.
In the following code the file is opened but not closed.
public sealed class Foo
{
public Foo()
{
using(var file = File.OpenRead("file.txt"))
{
...
}
}
}
Configure severity
Via ruleset file.
Configure the severity per project, for more info see MSDN.
Via #pragma directive.
#pragma warning disable IDISP004 // Don't ignore created IDisposable
Code violating the rule here
#pragma warning restore IDISP004 // Don't ignore created IDisposable
Or put this at the top of the file to disable all instances.
#pragma warning disable IDISP004 // Don't ignore created IDisposable
Via attribute [SuppressMessage].
[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness",
"IDISP004:Don't ignore created IDisposable",
Justification = "Reason...")]