Skip to content

Latest commit

 

History

History
86 lines (66 loc) · 2.14 KB

IDISP003.md

File metadata and controls

86 lines (66 loc) · 2.14 KB

IDISP003

Dispose previous before re-assigning

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

Description

Dispose previous before re-assigning.

Motivation

In the following example the old file will not be disposed when setting it to a new file in the Update method.

public sealed class Foo : IDisposable
{
    private FileStream stream = File.OpenRead("file.txt");

    public void Update(string fileName)
    {
        this.stream = File.OpenRead(fileName);
    }

    public void Dispose()
    {
        this.stream.Dispose();
    }
}

How to fix violations

Dispose the old value before assigning a new value.

public sealed class Foo : IDisposable
{
    private FileStream stream = File.OpenRead("file.txt");

    public void Update(string fileName)
    {
        this.stream?.Dispose();
        this.stream = File.OpenRead(fileName);
    }

    public void Dispose()
    {
        this.stream.Dispose();
    }
}

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable IDISP003 // Dispose previous before re-assigning
Code violating the rule here
#pragma warning restore IDISP003 // Dispose previous before re-assigning

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

#pragma warning disable IDISP003 // Dispose previous before re-assigning

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP003:Dispose previous before re-assigning", 
    Justification = "Reason...")]