Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MakeFieldReadonly false positive for ref assignments #58459

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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<ulong>();
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you have: readonly ref int x = ref y does this count as a read/write ref now? or just a read-ref? It should be the latter. Can you check, fix if necessary, and add test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➡️ Filed #58466 to resolve this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, agreed. This case should be readable reference, I'll make the change

}
else if (operation.Parent is IIncrementOrDecrementOperation)
{
return ValueUsageInfo.ReadWrite;
Expand Down