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 false positive when returning collection expressions #7508

Open
kasthack opened this issue Dec 16, 2024 · 0 comments
Open

CA2000 false positive when returning collection expressions #7508

kasthack opened this issue Dec 16, 2024 · 0 comments

Comments

@kasthack
Copy link

kasthack commented Dec 16, 2024

Analyzer

Diagnostic ID: CA2000: Call System.IDisposable.Dispose on object created by '...' before all references to it are out of scope (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: 9.0.101

Describe the bug

Returning a collection expression of IDisposable triggers CA2000 even though it shouldn't.

Steps To Reproduce

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <AnalysisLevel>latest</AnalysisLevel>
    <AnalysisMode>all</AnalysisMode>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
  </PropertyGroup>
</Project>

code:

using System.Data;
namespace CA2000;
public static class Repro
{
    public static DataColumn[] FalsePositive() =>
    [
        new DataColumn()    // <- triggers CA2000
    ];
    public static DataColumn[] NotTriggered() => new []
    {
        new DataColumn()    // works fine
    };
}

Expected behavior

FalsePositive method shouldn't trigger CA2000.

Actual behavior

1>C:\source\playground\src\Playground\CA2000\Program.cs(8,9,8,25): warning CA2000: Call System.IDisposable.Dispose on object created by 'new DataColumn()' before all references to it are out of scope (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