Repository navigation
Add CA1878 to prefer ReadOnlySpan properties over readonly array fields - #55901
Merged
tannergooding merged 5 commits intoAug 31, 2026
Merged
tannergooding merged 5 commits into
tannergooding merged 5 commits into
Conversation
…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]>
Co-authored-by: Copilot App <[email protected]>
|
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. |
Co-authored-by: Copilot App <[email protected]>
Copilot stopped reviewing on behalf of
tannergooding due to an error
August 24, 2026 01:00
tannergooding
requested
a balanced review from Copilot
and removed request for
Copilot
August 24, 2026 15:23
Contributor
There was a problem hiding this comment.
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
AsSpanrewrites. - 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
krwq
reviewed
Aug 25, 2026
krwq
reviewed
Aug 25, 2026
krwq
reviewed
Aug 25, 2026
krwq
reviewed
Aug 25, 2026
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 = $@" |
Member
There was a problem hiding this comment.
We should be using triple-quote strings consistently when defining code literals like this.
Member
Author
There was a problem hiding this comment.
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.
eiriktsarpalis
approved these changes
Aug 31, 2026
tannergooding
had a problem deploying
to
copilot-pat-pool
August 31, 2026 16:46 — with
GitHub Actions
Failure
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 supportsbool,byte, andsbyteon all targets withReadOnlySpan<T>, and supportsshort,ushort,char,int,uint,float,long,ulong, anddoublewhen the target exposesRuntimeHelpers.CreateSpan<T>(RuntimeFieldHandle).Documentation: dotnet/docs#55637.
Hardens the original implementation for expression trees, generated and partial code, nested and constructed generic references, attributes,
AsSpanoverloads, multi-declarator fields, trivia, and one-pass fix-all. The fixer reuses the existing statefulSyntaxEditorFixAllProviderpath rather than adding a parallel fix-all abstraction.Note
This pull request description was drafted with GitHub Copilot.