Skip to content

Do not trigger IDISP007 when instance is passed to private constructor by static method on the same type #587

@mgnslndh

Description

@mgnslndh

IDISP007 should not be triggered when an instance is created in a static method of a type and then passed to a private constructor of the same type.

namespace YourNamespace;

public sealed class MyFile : IDisposable
{
    private readonly LiteDatabase _database;

    private MyFile (LiteDatabase database)
    {
        _database = database;
    }

    public static MyFile Open(string filePath)
    {
        LiteDatabase? db;
        try
        {
            db = new LiteDatabase(filePath);
            // setup using db...
            return new MyFile (db);
        }
        catch
        {
            db?.Dispose();
            throw;
        }
    }

    public void Dispose()
    {
        // This triggers IDISP0007
        _database.Dispose();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions