diff --git a/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs b/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs index 35f6023dbe8b2..b8bd1b0369608 100644 --- a/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs +++ b/src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs @@ -3214,4 +3214,93 @@ class C public string Goo { get => field ?? throw new System.InvalidOperationException(); } = ""; } """ + s_allowNullAttribute); + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81320")] + public Task TestStaticFieldWrittenInInstanceConstructor_ReadOnlyProperty() + => TestInRegularAndScriptAsync( + """ + public sealed class Test + { + [|private static Test? s_instance;|] + public static Test Instance => s_instance!; + + public Test() + { + s_instance = this; + } + } + """, + """ + public sealed class Test + { + public static Test Instance { get => field!; private set; } + + public Test() + { + Instance = this; + } + } + """); + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/XXXXX")] + public Task TestStaticFieldWrittenInStaticConstructor_ReadOnlyProperty() + => TestInRegularAndScriptAsync( + """ + public sealed class Test + { + [|private static Test? s_instance;|] + public static Test Instance => s_instance!; + + static Test() + { + s_instance = new Test(); + } + } + """, + """ + public sealed class Test + { + public static Test Instance => field!; + + static Test() + { + Instance = new Test(); + } + } + """); + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/XXXXX")] + public Task TestStaticFieldWrittenInInstanceConstructor_WithSetter() + => TestInRegularAndScriptAsync( + """ + public sealed class Test + { + [|private static Test? s_instance;|] + public static Test Instance + { + get => s_instance!; + set => s_instance = value; + } + + public Test() + { + s_instance = this; + } + } + """, + """ + public sealed class Test + { + public static Test Instance + { + get => field!; + set; + } + + public Test() + { + Instance = this; + } + } + """); } diff --git a/src/Analyzers/Core/CodeFixes/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs index a702b894d8230..0ed1c2ac62ce2 100644 --- a/src/Analyzers/Core/CodeFixes/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs @@ -484,9 +484,12 @@ private async Task FormatAsync( CancellationToken cancellationToken) { var isWrittenOutsideConstructor = false; + + // Only include constructors that match the static-ness of the field. For a static field, only static + // constructors are relevant. For an instance field, only instance constructors are relevant. var constructorSpans = field.ContainingType .GetMembers() - .Where(m => m.IsConstructor()) + .Where(m => field.IsStatic ? m.IsStaticConstructor() : m.IsConstructor()) .SelectMany(c => c.DeclaringSyntaxReferences) .Select(s => s.GetSyntax(cancellationToken)) .Select(n => n.FirstAncestorOrSelf())