Repository navigation
Fix CA1825 false positive on collection expressions - #53521
Conversation
There was a problem hiding this comment.
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
InvalidCastExceptionby using anascast 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.
|
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? |
|
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. |
|
/backport to release/10.0.3xx |
|
Started backporting to |
|
@akhera99 backporting to 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 |
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.