Skip to content

Fix #72884: don't report IDE0052 on written-through ref-returning properties#84316

Draft
jcouv wants to merge 2 commits into
dotnet:mainfrom
jcouv:fix-issue-72884-ide0052-ref-property
Draft

Fix #72884: don't report IDE0052 on written-through ref-returning properties#84316
jcouv wants to merge 2 commits into
dotnet:mainfrom
jcouv:fix-issue-72884-ide0052-ref-property

Conversation

@jcouv

@jcouv jcouv commented Jun 28, 2026

Copy link
Copy Markdown
Member

Fixes #72884.

Problem

IDE0052 (Private member can be removed as the value assigned to it is never read) was a false positive on a private writable ref-returning property that is written through:

class C1
{
    int a;
    private ref int A => ref a;   // IDE0052 wrongly reported here
    public void Increment() => A++;
}

A++ invokes the property getter to obtain the ref int and writes through the returned reference. There is no setter, so the property is genuinely read on every access and IDE0052 must not fire. A = x and A += y were affected the same way, as were ref-returning indexers.

Fix

In AbstractRemoveUnusedMembersDiagnosticAnalyzer.AnalyzeMemberReferenceOperation, a writable ref-returning property/indexer reference always retains its Read bit (its getter is always invoked), so it is never downgraded to write-only:

if (memberSymbol is IPropertySymbol { RefKind: RefKind.Ref })
{
    valueUsageInfo |= ValueUsageInfo.Read;
}
else if (valueUsageInfo == ValueUsageInfo.ReadWrite)
{
    // existing ReadWrite -> Write downgrade heuristic (unchanged)
}

RefKind.RefReadOnly is correctly excluded (it cannot be written through). Genuinely unreferenced ref properties still surface IDE0051 since they never reach this method.

Tests

Added regression tests covering A++, A += y, A = x, and a ref-returning indexer (all expect no diagnostic), plus the existing read-only control. Verified ordinary write-only property increments still flag (no regression). Full RemoveUnusedMembers suite: 271 passed.

Microsoft Reviewers: Open in CodeFlow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1 participant