You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using System;
using System.Drawing;
namespace Ca2000FalsePositiveTestCase
{
internal static class Program
{
static void Main()
{
using (_ = new Bitmap(1, 1))
{
for (int i = 0; i < 1; ++i)
{
using (var bmp = new Bitmap(1, 1))
{
if (bmp.Tag is null)
throw new InvalidOperationException();
new Bitmap(1, 1).Dispose();
}
}
}
}
}
}
Expected behavior
No CA2000 warning.
Actual behavior
CA2000 is triggered:
Program.cs(14,38): error CA2000: Use recommended dispose pattern to ensure that object created by 'new Bitmap(1, 1)' is disposed on all paths. If possible, wrap the creation within a 'using' statement or a 'using' declaration. Otherwise, use a try-finally pattern, with a dedicated local variable declared before the try region and an unconditional Dispose invocation on non-null value in the 'finally' region, say 'x?.Dispose()'. If the object is explicitly disposed within the try region or the dispose ownership is transfered to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)
Additional context
The text was updated successfully, but these errors were encountered:
Analyzer
Diagnostic ID: CA2000:
Dispose objects before losing scope
Analyzer source
SDK: Built-in CA analyzers in .NET 5 SDK or later
Version: SDK 9.0.101
Describe the bug
The following program (see Steps To Reproduce) triggers CA2000 even though all disposable objects are disposed.
Steps To Reproduce
Ca2000FalsePositiveTestCase.csproj:
Program.cs:
Expected behavior
No CA2000 warning.
Actual behavior
CA2000 is triggered:
Additional context
The text was updated successfully, but these errors were encountered: