Skip to content

IDE0059 "Use discard" changes semantics with "using" #36502

@stephentoub

Description

@stephentoub

Version Used:
3.2.0-beta3-19307-02+a87b8ee11cb50f233d288cf86b99e315a408afa4

Steps to Reproduce:

  1. Use this program
using System;

class Program
{
    static void Main()
    {
        using var write = new WriteOnDispose();
        Console.WriteLine("almost done");
    }

    class WriteOnDispose : IDisposable
    {
        public void Dispose() => Console.WriteLine("done");
    }
}
  1. Refactor:
    image

  2. See the resulting program:

using System;

class Program
{
    static void Main()
    {
        _ = new WriteOnDispose();
        Console.WriteLine("almost done");
    }

    class WriteOnDispose : IDisposable
    {
        public void Dispose() => Console.WriteLine("done");
    }
}

Expected Behavior:
Either the refactoring shouldn't be offered, or it should be done in a way that doesn't remove the using. After refactoring the program should still output:

almost done
done

Actual Behavior:
The using gets removed, which means the disposable's Dispose method is never invoked. The refactored program only outputs:

almost done

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions