Skip to content

Fix CA1825 false positive on collection expressions - #53521

Merged
akhera99 merged 2 commits into
mainfrom
cs1825_false_pos
Mar 18, 2026
Merged

akhera99 merged 2 commits into
mainfrom
cs1825_false_pos

Conversation

@akhera99

Copy link
Copy Markdown
Member

Fixes dotnet/roslyn#82484

In .NET 11, the compiler lowers collection expressions and CA1825 incorrectly flagged these as zero-length array allocations, and the code fixer crashed with an InvalidCastException when the target type wasn't an array.

This adds a check in the analyzer to skip array creation operations whose syntax originates from a collection expression and switches from a direct cast to an as cast.

@akhera99
akhera99 requested a review from a team as a code owner March 18, 2026 17:44
Copilot AI review requested due to automatic review settings March 18, 2026 17:44

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

Fixes CA1825 incorrectly flagging compiler-synthesized array creations produced from C# collection expressions, and prevents the code fix from crashing when the target type isn’t an array.

Changes:

  • Update the analyzer to detect (and skip) array-creation operations that originate from collection expressions.
  • Harden the code fix to avoid InvalidCastException by using an as cast for the inferred array type.
  • Add C# 12 unit tests covering collection expressions with non-array and array targets.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

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 tests ensuring no CA1825 diagnostics are produced for collection-expression scenarios.
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.VisualBasic.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/BasicAvoidZeroLengthArrayAllocationsAnalyzer.vb Implements the new abstract hook for VB (returns False since VB has no collection expressions).
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidZeroLengthArrayAllocations.cs Adds collection-expression detection to suppress the false-positive CA1825 reports.
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/AvoidZeroLengthArrayAllocations.Fixer.cs Prevents code-fix crashes by safely casting to IArrayTypeSymbol.
src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.CSharp.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidZeroLengthArrayAllocations.cs Implements collection-expression syntax detection for C#.

You can also share your feedback on Copilot code review. Take the survey.

@akhera99
akhera99 merged commit 0f014ce into main Mar 18, 2026
27 checks passed
@akhera99
akhera99 deleted the cs1825_false_pos branch March 18, 2026 21:49
@AdamCoulterOz

Copy link
Copy Markdown
Contributor

Hi @akhera99 / @JoeRobich,

This looks like the right user-facing mitigation for CA1825, but it seems worth calling out that this is fixing the symptom at the analyzer layer rather than the underlying cause.

The analyzer is now explicitly excluding compiler-synthesized array creations whose syntax originates from a collection expression, which suggests Roslyn is surfacing a lowering artifact as an IArrayCreationOperation in a way analyzers can observe on non-array targets.

That may be pragmatic here, but it feels like the deeper question is whether this synthesized array should be exposed differently in the operation model, or at least marked distinctly enough that analyzers do not need collection-expression-specific exclusions.

Is there an upstream Roslyn tracking issue for that part?

@333fred

333fred commented Apr 13, 2026

Copy link
Copy Markdown
Member

There is nothing to do here from the Roslyn side. It is behaving exactly as expected: IOperation does not reflect the syntax used for an operation. It reflects the semantics. The semantic here is an array creation, so that's what you get from IOperation.

@akhera99

Copy link
Copy Markdown
Member Author

/backport to release/10.0.3xx

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0.3xx (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@akhera99 backporting to release/10.0.3xx failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: fix CA1825 false positive on collection expressions
Applying: review
error: sha1 information is lacking or useless (src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.CSharp.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/CSharpAvoidZeroLengthArrayAllocations.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 review
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

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.

[Regression] CA1825: False positive on .NET 11 Preview1

5 participants