Skip to content

IDE0032 code fixer produces invalid code for static field assigned in constructor #81320

@rampaa

Description

@rampaa

Version Used:
Visual Studio 2026 v18.0.0
.NET 10

Steps to Reproduce:

  1. Use the following minimal code sample:

    public sealed class Test
    {
        private static Test? s_instance;
        public static Test Instance => s_instance!;
    
        public Test()
        {
            s_instance = this;
        }
    }
  2. Observe that the analyzer reports IDE0032 for s_instance.

  3. Apply the code fix suggested by the IDE.

  4. Observe that the produced code cannot be compiled.

Diagnostic Id:
IDE0032: Use auto-implemented property

Expected Behavior:
The code fixer should not create invalid code that will not compile.

Actual Behavior:

The code fixer rewrites the class to:

public sealed class Test
{
    public static Test Instance => field!;

    public Test()
    {
        Instance = this;
    }
}

This results in the following compiler error:

CS0200: Property or indexer 'Test.Instance' cannot be assigned to — it is read only

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions