Skip to content

IDISP011 False positive when .Dispose is called in an event handler #571

Description

@hwndmaster

IDisposableAnalyzers version: 4.0.7

I have a scenario similar to this:

using System.Reactive;
using System.Reactive.Subjects;

namespace Demo;

internal sealed class Test
{
    private IObservable<Unit> _eventTriggered = new Subject<Unit>();

    public void Run()
    {
        using var foo = CreateFoo();
        // ...
    }

    private Foo CreateFoo()
    {
        var foo = new Foo();
        _eventTriggered.Subscribe(_ =>
        {
            // If the following line is commented, then no report of IDISP011:
            foo.Dispose();
        });
        return foo; // IDISP011: Don't return disposed instance
    }

    sealed class Foo : IDisposable
    {
        public void Dispose() { /* ... */ }
    }
}

foo can be disposed in two places: by the owner (for example, inside Run() method), or by some event, triggered somewhere else.

N.B.: Ignore other reports of IDISP004 or IDISP008, this is an example for IDISP011 only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions