Skip to content

CA1508 - False positive for null check in finally block #4672

@bartdesmet

Description

@bartdesmet
static void Repro()
{
    D d = null;
    try
    {
        d = new D();
    }
    finally
    {
        d?.Dispose();
    }
}

class D : IDisposable
{
    public D() => throw new InvalidOperationException();

    public void Dispose() { }
}

The constructor of D can throw and leave d to be null, so the finally block correctly checks for null. However, CA1508 suggests to remove the null check.

Metadata

Metadata

Assignees

Labels

BugThe product is not behaving according to its current intended designDataFlowFalse_PositiveA diagnostic is reported for non-problematic case

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions