Do not report MA0005 for the params array synthesized by a collection expression - #1531
Merged
Merged
Conversation
…ection expression A collection expression calls the constructor of the collection type without arguments, so the compiler synthesizes an empty array when that constructor has a params parameter (e.g. xUnit's TheoryData<T>). Users cannot replace it with Array.Empty<T>(). Fixes #1530
The arrays the compiler creates for a params parameter (method, constructor, indexer, attribute, or the constructor called by a collection expression) are implicit operations, so there is no need to match them against the arguments of the parent operation.
The array created by an array initializer (int[] a = { }) is an implicit
operation, but it is written by the user, so it must still be reported.
Add tests for array initializers, explicit arrays passed to params,
generic element types, and non-constant lengths.
meziantou
enabled auto-merge (squash)
September 18, 2026 21:14
This was referenced Sep 18, 2026
Closed
This was referenced Sep 26, 2026
Merged
Merged
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.
Fixes #1530
What changed
MA0005 (Use
Array.Empty<T>()) no longer reports the empty array that the compiler synthesizes for a collection expression.Why
A collection expression calls the collection type's constructor with no arguments, then
Addfor each element. When that constructor takes aparams T[]parameter, as in theTheoryData<T>of the issue, the compiler creates an empty array for it. The user cannot replace that array withArray.Empty<T>(), so the diagnostic was a false positive. MA0005 already skipped these synthesized arrays for method calls, constructor calls and indexers, but not for collection expressions.Notes for reviewers
IArrayCreationOperationwhereIsImplicitis true, since the user did not write that array and cannot replace it. The exception is an array initializer withoutnew(int[] a = { };): Roslyn marks it as implicit, but the user wrote it, so it is still reported.IsCompilerGeneratedParamsArray, which matched the synthesized array against the last argument of the parent method, constructor or indexer call and did not handle collection expressions. The existing tests for those cases and for implicitparamsarrays in attributes still pass.IsInAttributeis kept for an explicitnew int[0]inside an attribute, whereArray.Empty<T>()is not allowed.CollectionExpression_ParamsConstructor_ShouldNotReportError, which failed before the fix, and tests for array initializers (local and field), an explicit empty array passed toparams, a generic element type, and a non-constant length. TheUseArrayEmptyAnalyzerTestspass on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9.