roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

??= operator generates IDE0052 notice

Open ontorder opened this issue 2 years ago • 0 comments

Version Used: visual studio 17.7.6, framework net7.0

Steps to Reproduce: Given this code:

public class Test
{
    private System.Threading.Timer? _t;

    public async Task PublicMethodAsync(string p)
    {
        _t ??= new System.Threading.Timer(TimerCallback, p, 2000, -1);
    }

    private async void TimerCallback(object? state)
    {
        if (System.DateTime.Now.Millisecond % 2 == 0)
        {
            _t = null;
        }
    }
}

ide says field _t can be removed because it is never read. Using if (_t == null) _t = new Timer(...* or adding for example _t.Dispose() in cb results in no notice.

* btw i can see both versions are lowered to the same code.

Diagnostic Id: IDE0052

Expected Behavior: No suggestion should be given; removing field _t wouldn't be healthy. State of _t has to be read from multiple methods in different times, so context has to be maintained.

Actual Behavior: Ide is giving notice "Private member 'Test._t' can be removed as the value assigned to it is never read".

ontorder avatar Nov 08 '23 14:11 ontorder