You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>'.
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:
Treat property/indexer references used anywhere in a tuple/deconstruction assignment target as writable.
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.
Add a C# negative test for tuple swapping span elements and related nested target shapes.
Add a code-fix test proving no diagnostic/fix is offered when the replacement would cause CS8331.
Preserve existing read-only tuple/RHS behavior and all current direct-write controls.
Activity
jeremy-visionaid commentedon Mar 26, 2026
+1 Also seeing this
jeffhandley commentedon Aug 23, 2026
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
IsUsageSafereturns false:PreferReadOnlySpanOverSpan.cs, candidate trackingIsUsageSafeFor an indexer/property reference,
IsPropertyAccessSaferejects only these immediately recognized write shapes:IAssignmentOperationwhose target is the property;Source:
IsPropertyAccessSafeIn 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 levelGetValueUsageInfocheck also does not invalidate this shape, as demonstrated by the emitted diagnostic. No laterITupleOperationor 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:
PreferReadOnlySpanOverSpanTests.csLikely fix scope: