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

IMeterFactory.Create triggers false positive on IDisposable analysis #6912

Closed
noahfalk opened this issue Sep 6, 2023 · 1 comment
Closed
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting

Comments

@noahfalk
Copy link
Member

noahfalk commented Sep 6, 2023

Analyzer

Diagnostic ID: CA2000: Dispose objects before losing scope

Analyzer source

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

Describe the bug

.NET 8 adds a new API IMeterFactory.Create which returns instances of type Meter. Although the returned Meter object implements IDisposable, the IMeterFactory retains responsibility for invoking Dispose() when the factory is disposed. However the analyzer believes that the caller has the responsibility to call Dispose() and triggers a warning incorrectly.

Steps To Reproduce

  1. Create a new console project in VS
  2. Add this code into it (this is taken from the docs):
public class HatCoMetrics
{
    private readonly Counter<int> _hatsSold;

    public HatCoMetrics(IMeterFactory meterFactory)
    {
        var meter = meterFactory.Create("HatCo.Store");
        _hatsSold = meter.CreateCounter<int>("hatco.store.hats_sold");
    }

    public void HatsSold(int quantity)
    {
        _hatsSold.Add(quantity);
    }
}
  1. Change the analysis rules for the project to 'Latest all'
  2. Build

Expected behavior

No warning CA2000

Actual behavior

Warning CA2000 is generated by the build

@buyaa-n buyaa-n added Area-Microsoft.CodeAnalysis.NetAnalyzers help wanted The issue is up-for-grabs, and can be claimed by commenting False_Negative No diagnostic is reported for a problematic case labels Sep 19, 2023
@mavasani
Copy link
Contributor

CA2000 uses a heuristic to identify factory methods that create a disposable object that transfer dispose ownership, and static methods named Create that return an IDisposable are one such set. You should add source suppressions to suppress these for now. The core issue can only be resolved if we implement #1617 and dotnet/runtime#29631

Closing this as not-planned, but the above two issues track the core feature request here.

@mavasani mavasani closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

No branches or pull requests

3 participants