Roslyn: IDE0059 recommending to remove meaningful assignment

Created on 26 Feb 2020  路  4Comments  路  Source: dotnet/roslyn

Version Used:
Visual Studio 16.4.2

Steps to Reproduce:

public SqlConnection TryOpen()
{
    var keepOpen = false;
    var connection = new SqlConnection(new SqlConnectionStringBuilder { DataSource = @".\SQLEXPRESS", IntegratedSecurity = true }.ConnectionString);
    try
    {
        connection.Open();

        if (new Random().Next(2) == 1)
        {
            // keepOpen grayed out with suggestion to "Remove redundant assignment". 
            // If taken, removes this line with no other changes
            keepOpen = true;
            return connection;
        }
        else
        {
            return connection;
        }
    }
    finally
    {
        if (!keepOpen)
        {
            connection.Dispose();
        }
    }
}

Expected Behavior:

Assignment is meaningful since it flows into the finally block. Should be left alone.

Actual Behavior:

If suggestion is followed, code breaks.

Area-IDE Bug Resolution-Duplicate

All 4 comments

Duplicate of https://github.com/dotnet/roslyn/issues/39344. This was fixed in VS 16.5 Preview 2.

thanks @jnm2

Duplicate of #39344

Thanks @sharwell @jnm2 . I only searched open issues before posting. Good to know that this is fixed in the next version!

Was this page helpful?
0 / 5 - 0 ratings