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

False positive for CA2000 when disposable field is captured by an instance field #6760

Closed
CollinAlpert opened this issue Jul 12, 2023 · 2 comments
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design False_Positive A diagnostic is reported for non-problematic case Resolution-By Design
Milestone

Comments

@CollinAlpert
Copy link
Contributor

Analyzer

Diagnostic ID: CA2000: Dispose objects before losing scope

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 7.0.3

Describe the bug

CA2000 is emitted on an expression which is later captured by an instance field and then disposed.

Steps To Reproduce

using System;
using System.Net.Http;

class Test : IDisposable {
    private readonly HttpClient _client;

    public Test() {
        var handler = new HttpClientHandler(); // <-- CA2000 here
        _client = new HttpClient(handler, true);
    }

    public void Dispose()
    {
        _client.Dispose();
    }
}

Expected behavior

No diagnostic is emitted.

Actual behavior

warning CA2000: Call System.IDisposable.Dispose on object created by 'new HttpClientHandler()' before all references to it are out of scope

Additional context

@mavasani mavasani added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case labels Jul 13, 2023
@mavasani mavasani added this to the Unknown milestone Jul 13, 2023
@mavasani
Copy link
Contributor

@CollinAlpert This is by design, albeit not a very preferred one. #1617 discusses the core problem associated with dispose analysis and ownership, dotnet/runtime#29631 tracks some framework enhancements to help with this problem.

We do have an option to make the dispose analysis more conservative: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/Analyzer%20Configuration.md#configure-dispose-ownership-transfer-for-arguments-passed-to-constructor-invocation. I verified that setting this option avoids this false positive. Alternatively, we also have a list of well-known types whose constructor invocation indicates ownership transfer, so this one could be one more addition:

private static readonly string[] s_disposeOwnershipTransferLikelyTypes = new string[]
{
"System.IO.Stream",
"System.IO.TextReader",
"System.IO.TextWriter",
"System.Resources.IResourceReader",
};
.
However, having such well-known type list is not very scalable, and hence not something we have added to that often. The rule in it's current state is just a port of the old CA2000 to detect basic leaks, but we would need a much better design and framework support for implementing an analysis that is much more reliable and precise.

@CollinAlpert
Copy link
Contributor Author

Thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design False_Positive A diagnostic is reported for non-problematic case Resolution-By Design
Projects
None yet
Development

No branches or pull requests

2 participants