Skip to content

[11.0-preview.2] CA1517 false positive #53388

Closed

Description

@martincostello

Describe the bug

A method that writes to a Span<char> generates a false-positive warning of warning CA1517: Parameter 'values' can be declared as 'ReadOnlySpan<char>' instead of as 'Span<char>'.

To Reproduce

Run the following program:

#:property AnalysisMode=All

Span<char> values = ['A', 'B', 'C', 'D'];
Console.WriteLine(values); // Output: ABCD

Swapper.Swap(values, 1, 2);
Console.WriteLine(values); // Output: ACBD

internal static class Swapper
{
    public static void Swap(Span<char> values, int x, int y)
    {
        (values[x], values[y]) = (values[y], values[x]);
    }
}

Exceptions (if any)

None.

Further technical details

.NET SDK 11.0.100-preview.2.26159.112

Activity

  1. added
    untriaged
    on Mar 11, 2026
  2. jeremy-visionaid commented on Mar 26, 2026

    @jeremy-visionaid

    +1 Also seeing this

  3. jeffhandley commented on Aug 23, 2026

    @jeffhandley
    Member

    Thanks for reporting this, @martincostello and @jeremy-visionaid. I verified the repro.

    Root cause

    Confidence: high, based on the independent repro and the current analyzer source.

    The analyzer registers an action for every candidate parameter reference and removes the parameter only when IsUsageSafe returns false:

    For an indexer/property reference, IsPropertyAccessSafe rejects only these immediately recognized write shapes:

    • direct parent IAssignmentOperation whose target is the property;
    • increment/decrement;
    • ref/out argument;
    • ref local;
    • ref return.

    Source: IsPropertyAccessSafe

    In a tuple/deconstruction assignment, the indexed elements are nested under the tuple target rather than having the assignment as their immediate parent. The direct-parent check therefore misses the write and the method returns true. The parameter-reference level GetValueUsageInfo check also does not invalidate this shape, as demonstrated by the emitted diagnostic. No later ITupleOperation or deconstruction-assignment handling recovers the write.

    Existing tests cover simple index assignment, compound assignment, prefix/postfix increment, ref/out, ref locals/returns, and chained slices, but no tuple/deconstruction assignment:

    Likely fix scope:

    1. Treat property/indexer references used anywhere in a tuple/deconstruction assignment target as writable.
    2. Prefer a value-usage-based check that is correct through wrapper operations if the Roslyn API supports the needed shape; otherwise walk tuple/deconstruction ancestors explicitly.
    3. Add a C# negative test for tuple swapping span elements and related nested target shapes.
    4. Add a code-fix test proving no diagnostic/fix is offered when the replacement would cause CS8331.
    5. Preserve existing read-only tuple/RHS behavior and all current direct-write controls.
  4. added and removed
    untriaged
    on Aug 23, 2026
  5. added this to the 11.0.1xx milestone on Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions