Skip to content

"Field is never assigned" warning inconsistent for ref fields #75315

Closed
@jjonescz

Description

@jjonescz

Version Used: 4.13.0-1.24480.10 (ba7fe35)

Steps to Reproduce:

ref struct R
{
    public ref int F; // warning CS0649: Field 'R.F' is never assigned to, and will always have its default value 0

    static void M(in int x) { }

    static void Test(R r)
    {
        M(r.F);
    }
}

compare with:

ref struct R
{
    public ref int F; // no warning

    static void M(in int x) { }

    static void Test(R r)
    {
        M(in r.F); // `in` added here
    }
}

Expected Behavior: The two code snippets are equivalent, so I would expect the warning to be either reported for both or for none of them.

Actual Behavior: The warning is reported inconsistently.

Additional Notes:

  • The warning is also not reported for other callsite modifiers like ref (but one cannot reproduce the "no callsite modifier" scenario there). Perhaps the warning is not supposed to be reported for ref fields at all.
  • If the warning should be reported for ref fields, it shouldn't say that R.F will always have its default value 0 - because it's a ref field, it will have its default value null.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions