Skip to content

[release/10.0.3xx] Fix CA1825 false positive on collection expressions - #54294

Merged
akhera99 merged 2 commits into
release/10.0.3xxfrom
backport/53521
May 14, 2026
Merged

[release/10.0.3xx] Fix CA1825 false positive on collection expressions#54294
akhera99 merged 2 commits into
release/10.0.3xxfrom
backport/53521

Conversation

@akhera99

@akhera99 akhera99 commented May 13, 2026

Copy link
Copy Markdown
Member

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.

Copilot AI review requested due to automatic review settings May 13, 2026 16:29
@akhera99
akhera99 requested a review from a team as a code owner May 13, 2026 16:29
@akhera99
akhera99 requested a review from jjonescz May 13, 2026 16:29

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

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 as cast 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

@akhera99 akhera99 changed the title [Backport release/10.0.3xx] Fix CA1825 false positive on collection expressions [release/10.0.3xx] Fix CA1825 false positive on collection expressions May 13, 2026
@akhera99
akhera99 merged commit 5f13d9a into release/10.0.3xx May 14, 2026
32 checks passed
@akhera99
akhera99 deleted the backport/53521 branch May 14, 2026 20:08
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone May 27, 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.

3 participants