IDISP007

November 25, 2021 ยท View on GitHub

Don't dispose injected

TopicValue
IdIDISP007
SeverityWarning
EnabledTrue
CategoryIDisposableAnalyzers.Correctness
CodeDisposeCallAnalyzer
LocalDeclarationAnalyzer
UsingStatementAnalyzer

Description

Don't dispose disposables you do not own.

Motivation

Disposing IDisposables that you have not created and do not own can be a bug.

How to fix violations

Don't dispose them.

Configure severity

Via ruleset file.

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

Via #pragma directive.

#pragma warning disable IDISP007 // Don't dispose injected
Code violating the rule here
#pragma warning restore IDISP007 // Don't dispose injected

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

#pragma warning disable IDISP007 // Don't dispose injected

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("IDisposableAnalyzers.Correctness", 
    "IDISP007:Don't dispose injected", 
    Justification = "Reason...")]