Skip to content

Latest commit

 

History

History
63 lines (49 loc) · 1.88 KB

IDISP001.md

File metadata and controls

63 lines (49 loc) · 1.88 KB

IDISP001

Dispose created

Topic Value
Id IDISP001
Severity Warning
Enabled True
Category IDisposableAnalyzers.Correctness
Code ArgumentAnalyzer
AssignmentAnalyzer
LocalDeclarationAnalyzer

Description

When you create an instance of a type that implements IDisposable you are responsible for disposing it. This rule will warn even if you have an explicit dispose call and try finally. The reason for not filtering out those cases is that using reads better.

Motivation

The following example will leave the file open.

var reader = new StreamReader(fileName);
return reader.ReadLine();

How to fix violations

using (var reader = new StreamReader(fileName))
{
    return reader.ReadLine();
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable IDISP001 // Dispose created
Code violating the rule here
#pragma warning restore IDISP001 // Dispose created

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

#pragma warning disable IDISP001 // Dispose created

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP001:Dispose created", 
    Justification = "Reason...")]