Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA2000 possible false positive #7527

Open
zwergenaufstand opened this issue Jan 13, 2025 · 0 comments
Open

CA2000 possible false positive #7527

zwergenaufstand opened this issue Jan 13, 2025 · 0 comments

Comments

@zwergenaufstand
Copy link

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

  1. Compile the following program targeting .NET Framework 4.8.

Ca2000FalsePositiveTestCase.csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Exe</OutputType>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisLevel>latest-all</AnalysisLevel>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
    <WarningLevel>4</WarningLevel>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
</Project>

Program.cs:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant