Skip to content

Do not report MA0005 for arrays of pointers - #1460

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0005-pointer-array-generic-bd2d28
Sep 12, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0005-pointer-array-generic-bd2d28

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Problem

MA0005 reported zero-length arrays of pointers, and its code fix produced code that does not compile:

public unsafe class Sample
{
    public static int*[] Run() => new int*[0];
}

became

public static int*[] Run() => System.Array.Empty<int*>(); // CS0306

Pointer and function pointer types cannot be used as generic type arguments.

Change

  • UseArrayEmptyAnalyzer no longer reports when the array's element type is a pointer or a function pointer. The code fix only runs on this diagnostic, so it is no longer offered either.
  • Arrays whose element type is itself an array of pointers (new int*[0][]) are still reported, since Array.Empty<int*[]>() is valid.
  • docs/Rules/MA0005.md documents the exclusion.

Tests

  • EmptyPointerArray_ShouldNotReportError: new int*[0], new int*[] { }, new delegate*<void>[0]
  • EmptyArrayOfPointerArrays_ShouldReportError: new int*[0][] is still fixed to System.Array.Empty<int*[]>()

All 12 UseArrayEmptyAnalyzerTests pass on Roslyn 5.9 and 4.8. With the analyzer change reverted, the three pointer cases fail. I ran only these tests locally; CI runs the other Roslyn versions.

Pointer and function pointer types cannot be used as generic type
arguments, so replacing `new int*[0]` with `Array.Empty<int*>()`
produced code that fails with CS0306.
@meziantou
meziantou merged commit 9024278 into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0005-pointer-array-generic-bd2d28 branch September 12, 2026 00:42
This was referenced Sep 12, 2026
This was referenced Sep 26, 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.

1 participant