dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.09k stars 4.04k forks source link

Incorrect diagnostic that Deconstruct method is unused. #75995

Closed gafter closed 19 hours ago

gafter commented 1 day ago

Roslyn reports that Deconstruct is unused in the C# code below. But it is used. You would get an error if you remove the method.

class C
{
    void M(
        ref object o,
        ref object p)
    {
        (o, p) = this;
    }

    void Deconstruct(
        out object? o,
        out object? p)
    {
        o = null;
        p = null;
    }
}
CyrusNajmabadi commented 1 day ago

@gafter are you getting a compiler warning, or an analyzer warning? If so, can you include the diagnostic id?

gafter commented 1 day ago

I don't know. See https://sharplab.io/#v2:CYLg1APgAgTABAYQLACgDeq5blALHAWQApNsyAnAUwDM4B7AIwCtKBjAF3oBpSysrajFhzgAHAJS84GFH2xE6XMeLgBeOOwAWASwDOAbikBfKVLxwAImzoA7Xe3IBXDiVlz6jzkLbsA/NykyOk96Zh9/CSkZd3o1OBtHABtEwzc5UTiE5NSyExQ8oA==

I think it's a compiler informational message.

jcouv commented 22 hours ago

I see that diagnostic too (IDE0051) Image

CyrusNajmabadi commented 20 hours ago

@jcouv I can't figure out how to determine that the .Deconstruct method is used here. How is this exposed in teh semantic model? Also checking with @333fred . I do see a IDeconstructAssignmentOperation, but it seems to be missing the target method, unless i'm missing something. Could you guys advise? How do you encode this into the tree?

jcouv commented 20 hours ago

There's an issue tracking adding this information to IDeconstructAssignmentOperation: https://github.com/dotnet/roslyn/issues/74757 Otherwise, GetDeconstructionInfo and GetDeconstructionInfo on semantic model currently should have the information.