Skip to content

Add CA1878 to prefer ReadOnlySpan properties over readonly array fields - #55901

Merged
tannergooding merged 5 commits into
dotnet:mainfrom
tannergooding:tannergooding-port-readonlyspan-analyzer
Aug 31, 2026
Merged

tannergooding merged 5 commits into
dotnet:mainfrom
tannergooding:tannergooding-port-readonlyspan-analyzer

Conversation

@tannergooding

@tannergooding tannergooding commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

This continues the work originally contributed by @NewellClark in dotnet/roslyn-analyzers#5548, carrying it into the migrated NetAnalyzers as CA1878.

The rule recommends replacing eligible private static readonly constant-array fields with ReadOnlySpan<T> properties so the C# compiler can lower the data without a managed array allocation. It supports bool, byte, and sbyte on all targets with ReadOnlySpan<T>, and supports short, ushort, char, int, uint, float, long, ulong, and double when the target exposes RuntimeHelpers.CreateSpan<T>(RuntimeFieldHandle).

Documentation: dotnet/docs#55637.


Hardens the original implementation for expression trees, generated and partial code, nested and constructed generic references, attributes, AsSpan overloads, multi-declarator fields, trivia, and one-pass fix-all. The fixer reuses the existing stateful SyntaxEditorFixAllProvider path rather than adding a parallel fix-all abstraction.

Note

This pull request description was drafted with GitHub Copilot.

tannergooding and others added 3 commits August 22, 2026 22:14
…ly array fields

Ports dotnet/roslyn-analyzers#5548 into the migrated NetAnalyzers, using
the next free Performance id CA1878 (the original CA1862 is already taken).

Co-authored-by: Newell Clark <[email protected]>
Co-authored-by: Copilot App <[email protected]>
…Provider

The first three also reproduce against the original roslyn-analyzers
implementation:

- Fields referenced inside an expression tree were flagged, and converting
  one produced CS8640.
- The fixer crashed on fields passed as named or reordered AsSpan arguments.
- Converting a field silently dropped its attributes.

Fix-all separately corrupted fields declaring several diagnosed variables,
because SeparatedSyntaxList.Remove silently no-ops once an earlier removal
has rebuilt the list.

Co-authored-by: Copilot App <[email protected]>
Copilot AI lite review requested due to automatic review settings August 24, 2026 00:39
@tannergooding
tannergooding requested a review from a team as a code owner August 24, 2026 00:39
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 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 stopped reviewing on behalf of tannergooding due to an error August 24, 2026 01:00
@tannergooding
tannergooding requested a balanced review from Copilot and removed request for Copilot August 24, 2026 15:23

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.

Pull request overview

Adds CA1878 to detect eligible readonly array fields and replace them with allocation-free ReadOnlySpan<T> properties.

Changes:

  • Adds the analyzer, C# fixer, and extensive regression tests.
  • Supports one-pass fix-all, attributes, generics, partial code, and AsSpan rewrites.
  • Registers the rule and generates documentation/localization artifacts.
Show a summary per file
File Description
.../PreferReadOnlySpanPropertiesOverReadOnlyArrayFieldsTests.cs Adds analyzer and fixer tests.
.../WellKnownTypeNames.cs Registers RuntimeHelpers.
.../DiagnosticCategoryAndIdRanges.txt Extends Performance IDs.
.../SyntaxEditorFixAllProvider.cs Exposes stateful fix application internally.
...Resources.zh-Hant.xlf Adds localized resources.
...Resources.zh-Hans.xlf Adds localized resources.
...Resources.tr.xlf Adds localized resources.
...Resources.ru.xlf Adds localized resources.
...Resources.pt-BR.xlf Adds localized resources.
...Resources.pl.xlf Adds localized resources.
...Resources.ko.xlf Adds localized resources.
...Resources.ja.xlf Adds localized resources.
...Resources.it.xlf Adds localized resources.
...Resources.fr.xlf Adds localized resources.
...Resources.es.xlf Adds localized resources.
...Resources.de.xlf Adds localized resources.
...Resources.cs.xlf Adds localized resources.
.../PreferReadOnlySpanPropertiesOverReadOnlyArrayFieldsAnalyzer.cs Implements CA1878 analysis.
.../MicrosoftNetCoreAnalyzersResources.resx Defines rule and fixer strings.
.../AnalyzerReleases.Unshipped.md Registers CA1878 for release.
.../Microsoft.CodeAnalysis.NetAnalyzers.sarif.template Adds CA1878 SARIF metadata.
.../Microsoft.CodeAnalysis.NetAnalyzers.md Adds generated rule documentation.
.../CSharpPreferReadOnlySpanPropertiesOverReadOnlyArrayFields.Fixer.cs Implements the C# fixer.

Review details

  • Files reviewed: 23/23 changed files
  • Comments generated: 3
  • Review effort level: Balanced

Gate the rule on C# 7.2, support safe foreach uses and aliased or nullable array declarations, and reuse existing primitive and type helpers.

Co-authored-by: Copilot App <[email protected]>
{
var test = new VerifyCS.Test
{
TestCode = $@"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should be using triple-quote strings consistently when defining code literals like this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will handle this in a follow up and across all the tests. I don't think we're entirely consistent here today and it mostly depends on whether or not the code contains quotes itself.

@tannergooding
tannergooding merged commit 1939534 into dotnet:main Aug 31, 2026
39 checks passed
@tannergooding
tannergooding deleted the tannergooding-port-readonlyspan-analyzer branch August 31, 2026 16:46
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 2, 2026

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — c9c88734 Deployed Aug 31, 2026 by tannergooding via pre_activation #1946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants