Skip to content

"Convert to 'field' property" changes semantics when get-only property has same name as primary constructor parameter #80771

@jhinder

Description

@jhinder

Version Used: VS 18.0.0 Insiders [11111.16]

Steps to Reproduce:

  1. Open the lightbulb menu at the marked location.
  2. Select "Convert to 'field' property"
record C(int P)
{
    public int $$P { get; } = P;
}

Expected Behavior:
One of the following:

  • The modified code retains the semantics of the original.
  • If this is not possible with this refactoring, it is not offered in this context, or a warning about potentially changing semantics is shown in the preview popover.

Actual Behavior:

record C(int P) // CS8907 Parameter 'P' is unread. Did you forget to use it to initialize the property with that name?
{
    public int P => field = P;
}

Note that read-write auto-property are converted correctly:

record C(int P)
{
    public int P { get => field; set => field = value; } = P;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions