Skip to content

Commit f6824bc

Browse files
Fix IDE0032: Code fixer adds setter for static properties with instance constructor writes (#81327)
2 parents e1008e6 + 2e46dba commit f6824bc

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/Analyzers/CSharp/Tests/UseAutoProperty/UseAutoPropertyTests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,4 +3214,93 @@ class C
32143214
public string Goo { get => field ?? throw new System.InvalidOperationException(); } = "";
32153215
}
32163216
""" + s_allowNullAttribute);
3217+
3218+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81320")]
3219+
public Task TestStaticFieldWrittenInInstanceConstructor_ReadOnlyProperty()
3220+
=> TestInRegularAndScriptAsync(
3221+
"""
3222+
public sealed class Test
3223+
{
3224+
[|private static Test? s_instance;|]
3225+
public static Test Instance => s_instance!;
3226+
3227+
public Test()
3228+
{
3229+
s_instance = this;
3230+
}
3231+
}
3232+
""",
3233+
"""
3234+
public sealed class Test
3235+
{
3236+
public static Test Instance { get => field!; private set; }
3237+
3238+
public Test()
3239+
{
3240+
Instance = this;
3241+
}
3242+
}
3243+
""");
3244+
3245+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/XXXXX")]
3246+
public Task TestStaticFieldWrittenInStaticConstructor_ReadOnlyProperty()
3247+
=> TestInRegularAndScriptAsync(
3248+
"""
3249+
public sealed class Test
3250+
{
3251+
[|private static Test? s_instance;|]
3252+
public static Test Instance => s_instance!;
3253+
3254+
static Test()
3255+
{
3256+
s_instance = new Test();
3257+
}
3258+
}
3259+
""",
3260+
"""
3261+
public sealed class Test
3262+
{
3263+
public static Test Instance => field!;
3264+
3265+
static Test()
3266+
{
3267+
Instance = new Test();
3268+
}
3269+
}
3270+
""");
3271+
3272+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/XXXXX")]
3273+
public Task TestStaticFieldWrittenInInstanceConstructor_WithSetter()
3274+
=> TestInRegularAndScriptAsync(
3275+
"""
3276+
public sealed class Test
3277+
{
3278+
[|private static Test? s_instance;|]
3279+
public static Test Instance
3280+
{
3281+
get => s_instance!;
3282+
set => s_instance = value;
3283+
}
3284+
3285+
public Test()
3286+
{
3287+
s_instance = this;
3288+
}
3289+
}
3290+
""",
3291+
"""
3292+
public sealed class Test
3293+
{
3294+
public static Test Instance
3295+
{
3296+
get => field!;
3297+
set;
3298+
}
3299+
3300+
public Test()
3301+
{
3302+
Instance = this;
3303+
}
3304+
}
3305+
""");
32173306
}

src/Analyzers/Core/CodeFixes/UseAutoProperty/AbstractUseAutoPropertyCodeFixProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,12 @@ private async Task<SyntaxNode> FormatAsync(
484484
CancellationToken cancellationToken)
485485
{
486486
var isWrittenOutsideConstructor = false;
487+
488+
// Only include constructors that match the static-ness of the field. For a static field, only static
489+
// constructors are relevant. For an instance field, only instance constructors are relevant.
487490
var constructorSpans = field.ContainingType
488491
.GetMembers()
489-
.Where(m => m.IsConstructor())
492+
.Where(m => field.IsStatic ? m.IsStaticConstructor() : m.IsConstructor())
490493
.SelectMany(c => c.DeclaringSyntaxReferences)
491494
.Select(s => s.GetSyntax(cancellationToken))
492495
.Select(n => n.FirstAncestorOrSelf<TConstructorDeclaration>())

0 commit comments

Comments
 (0)