[release/10.0.3xx] Fix CA1825 false positive on collection expressions - #54294
Merged
Conversation
jjonescz
approved these changes
May 13, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Backports #53521 to release/10.0.3xx to address a CA1825 false positive (and related code-fix crash) caused by C# collection expressions lowering to compiler-generated array creation operations.
Changes:
- Skip CA1825 analysis for array-creation operations whose syntax originates from a C# collection expression.
- Make the code fix resilient to non-array target types by switching to a safe
ascast when deriving the array element type. - Add regression tests covering collection-expression scenarios (non-array target, array target, and explicit
new int[0]inside a collection expression).
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidZeroLengthArrayAllocationsTests.cs | Adds C#12 collection-expression regression tests (no diagnostic for lowered compiler arrays; diagnostic still for explicit new int[0]). |
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/BasicAvoidZeroLengthArrayAllocationsAnalyzer.vb | Implements the new collection-expression hook for VB (returns False, since VB has no collection expressions). |
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidZeroLengthArrayAllocations.Fixer.cs | Prevents InvalidCastException by using as IArrayTypeSymbol when extracting element type. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidZeroLengthArrayAllocations.cs | Adds IsCollectionExpressionSyntax abstraction and bails out early for collection-expression-originated syntax. |
| src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.CSharp.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidZeroLengthArrayAllocations.cs | Implements IsCollectionExpressionSyntax for C# via SyntaxKindEx.CollectionExpression. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 0
This was referenced May 21, 2026
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.
Backports: #53521
Original issue: dotnet/roslyn#82484
Description
Fixes #54275 where, starting in 10.0.3xx, the compiler lowers collection expressions and CA1825 incorrectly flagged these as zero-length array allocations. The code fixer crashed with an InvalidCastException when the target type wasn't an array. This PR adds a check to bail out when the array creation syntax originates from a collectionexpression, and switches from a direct cast to an as cast in the fixer.
Customer impact
Customer-reported (#54275, #53047). Users on SDK 10.0.300 see CA1825 warnings on any code using collection expressions. The fixer also crashes, making the experience worse.
Regression
Yes, this regressed in 10.0.300. The compiler's collection-expression lowering is new, and the existing CA1825 analyzer didn't account for it.
Risk
Low. The change is small, tightly scoped to the CA1825 analyzer/fixer, does not affect other analyzers or SDK behavior, and includes three new unit tests covering the scenarios.