diff --git a/src/Analyzers/CSharp/Tests/MakeFieldReadonly/MakeFieldReadonlyTests.cs b/src/Analyzers/CSharp/Tests/MakeFieldReadonly/MakeFieldReadonlyTests.cs index 811f51493f746..9a7d768db6754 100644 --- a/src/Analyzers/CSharp/Tests/MakeFieldReadonly/MakeFieldReadonlyTests.cs +++ b/src/Analyzers/CSharp/Tests/MakeFieldReadonly/MakeFieldReadonlyTests.cs @@ -1717,13 +1717,13 @@ await TestInRegularAndScript1Async( } [WorkItem(46785, "https://github.com/dotnet/roslyn/issues/46785")] - [Fact(Skip = "https://github.com/dotnet/roslyn/issues/46785"), Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)] public async Task UsedAsRef_NoDiagnostic() { await TestMissingInRegularAndScriptAsync( @"public class C { - private string [|_x|] = string.Empty; + private string [|x|] = string.Empty; public bool M() { @@ -1733,6 +1733,26 @@ public bool M() }"); } + [WorkItem(57983, "https://github.com/dotnet/roslyn/issues/57983")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)] + public async Task UsedAsRef_NoDiagnostic_02() + { + await TestMissingInRegularAndScriptAsync( +@"using System.Runtime.CompilerServices; + +public class Test +{ + private ulong [|nextD3D12ComputeFenceValue|]; + + internal void Repro() + { + ref ulong d3D12FenceValue = ref Unsafe.NullRef(); + d3D12FenceValue = ref nextD3D12ComputeFenceValue; + d3D12FenceValue++; + } +}"); + } + [WorkItem(42760, "https://github.com/dotnet/roslyn/issues/42760")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)] public async Task WithThreadStaticAttribute_NoDiagnostic() diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/OperationExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/OperationExtensions.cs index 800e930574599..3bde34e3df8b5 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/OperationExtensions.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/OperationExtensions.cs @@ -126,6 +126,12 @@ INegatedPatternOperation or ? ValueUsageInfo.ReadWrite : ValueUsageInfo.Write; } + else if (operation.Parent is ISimpleAssignmentOperation simpleAssignmentOperation && + simpleAssignmentOperation.Value == operation && + simpleAssignmentOperation.IsRef) + { + return ValueUsageInfo.ReadableWritableReference; + } else if (operation.Parent is IIncrementOrDecrementOperation) { return ValueUsageInfo.ReadWrite;