BADF00D / DisposableFixer

This is a Visual Studio Extension and NuGet package that should identify and fix problems as memleaks while using IDisposables.
Other
35 stars 7 forks source link

Conditional object creation without Using statement incorrectly warns #131

Closed Cassandra-d closed 4 years ago

Cassandra-d commented 4 years ago

Prerequisites

Description

When there is conditional object instantiation and Dispose is called on the object afterwards. Actual: warning "This ObjectCreation returns an object that is not disposed". Expected: no warning, as Dispose is called.

Source Code

static NamedPipeServerStream _pipe;
static void Main(string[] args)
{
    StreamWriter sw = _pipe != null ? new StreamWriter(_pipe) { AutoFlush = true } : null;
    try
    {
    }
    finally
    {
        sw?.Dispose();
    }
}

Screenshot

dispose2