Skip to content

Fix CA1517 for mutable span enumeration - #56393

Merged
tannergooding merged 2 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/fix-ca1517-foreach-refs
Sep 25, 2026
Merged

tannergooding merged 2 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/fix-ca1517-foreach-refs

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Summary

  • treat mutable foreach (ref ...) iteration over Span<T> as a writable use for CA1517
  • follow foreach collection conversions without confusing unrelated references in the loop body
  • reject method substitutions whose read-only counterpart changes the return contract, including writable span enumerators
  • add regression coverage for mutable, read-only, sliced, explicitly converted, and manual enumeration shapes

Fixes #56292

Testing

  • .\.dotnet\dotnet.exe scripts\RunTests.cs -- --project src\Microsoft.CodeAnalysis.NetAnalyzers\tests\Microsoft.CodeAnalysis.NetAnalyzers.UnitTests\Microsoft.CodeAnalysis.NetAnalyzers.UnitTests.csproj --filter "FullyQualifiedName~PreferReadOnlySpanOverSpanTests" --skip-redist-check (111 passed)
  • .\build.cmd -projects src\Microsoft.CodeAnalysis.NetAnalyzers\Microsoft.CodeAnalysis.NetAnalyzers.slnx -c Debug (0 warnings, 0 errors)

Review

  • completed two independent review rounds
  • addressed findings for manual writable enumerators, nested collection conversions, and unrelated span reads inside mutable ref-foreach bodies

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 24, 2026 14:08
@Evangelink
Evangelink requested a review from a team as a code owner September 24, 2026 14:08
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved issues were identified that would block approval.

Review effort: Lite
Findings: None

What changed in this PR

Fixes CA1517 false positives for writable ref foreach enumeration over spans and prevents unsafe method substitutions.

Changes:

  • Tracks foreach collection conversions and writable loop variables.
  • Validates substituted method return types.
  • Adds regression tests for span enumeration scenarios.
File Description
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​Performance/​PreferReadOnlySpanOverSpanTests.cs Updated as part of this pull request.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​src/​Microsoft.CodeAnalysis.NetAnalyzers/​Microsoft.NetCore.Analyzers/​Performance/​PreferReadOnlySpanOverSpan.cs Updated as part of this pull request.

@tannergooding
tannergooding merged commit a7ca4d4 into dotnet:main Sep 25, 2026
23 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/fix-ca1517-foreach-refs branch September 25, 2026 18:58
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA1517 false positive when span elements are mutated via foreach (ref var ...)

3 participants