Skip to content

CA2213 false positive on explicit IAsyncDisposable interface implementation #6075

@wiciok

Description

@wiciok

Analyzer

Diagnostic ID: CA2213: Disposable fields should be disposed

Analyzer source

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

Version: 6.0.302

Describe the bug

CA2213 doesn't recognize Dispose / DisposeAsync calls made in explicitly implemented IAsyncDisposable.DisposeAsync method, resulting in false-positive. This happens only when both IAsyncDisposable and IDisposable interfaces are implemented.

image

Steps To Reproduce

Run analyzers on following code sample:

namespace IAsyncDisposableBug
{
    public sealed class Test : IAsyncDisposable, IDisposable
    {
        private readonly HttpClient client;
        private readonly FileStream stream;

        public Test()
        {
            client = new HttpClient();
            stream = new FileStream("C://some-path", FileMode.CreateNew);
        }

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

        async ValueTask IAsyncDisposable.DisposeAsync()
        {
            await stream.DisposeAsync();
        }
    }
}

Expected behavior

Dispose/DisposeAsync calls are properly recognized regardless of how DisposeAsync() is implemented

Actual behavior

Dispose/DisposeAsync calls are not properly recognized when made from explicitly implemented IAsyncDisposable.DisposeAsync()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Microsoft.CodeAnalysis.NetAnalyzersBugThe product is not behaving according to its current intended designFalse_PositiveA diagnostic is reported for non-problematic casehelp wantedThe issue is up-for-grabs, and can be claimed by commenting

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions