Skip to content

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

@bkoelman

Description

@bkoelman

Version Used:
.NET 11 Preview 1 with VS Insiders 2026 [11513.90]

This is a regression. The problem does not occur using stable .NET 10 and stable VS 2026.

Steps to Reproduce:
Using xUnit, a suggestion is reported on the following lines:

public sealed class ModelStateValidationTests : IClassFixture<OpenApiTestContext>
{
    public static readonly TheoryData<string> SchemaNames =
    [ /* SUGGESTION HERE */
        "attributesInCreateTypeContainerRequest",
        "attributesInUpdateTypeContainerRequest",
        "attributesInTypeContainerResponse"
    ];
}

Applying the fix changes nothing. After applying it, the fix disappears from the context menu.
The following popup is displayed:

Image

Message from the screenshot:

System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.NonErrorNamedTypeSymbol' to type 'Microsoft.CodeAnalysis.IArrayTypeSymbol'.
   at Microsoft.NetCore.Analyzers.Runtime.AvoidZeroLengthArrayAllocationsFixer.GetArrayElementType(SyntaxNode arrayCreationExpression,SemanticModel semanticModel,CancellationToken cancellationToken)
   at async Microsoft.NetCore.Analyzers.Runtime.AvoidZeroLengthArrayAllocationsFixer.ConvertToArrayEmptyAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.NetCore.Analyzers.Runtime.AvoidZeroLengthArrayAllocationsFixer.<>c__DisplayClass4_0.<RegisterCodeFixesAsync>b__0(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.DocumentChangeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.EditorSuggestedAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.EditorSuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.EditorSuggestedActionWithNestedFlavors.<>c__DisplayClass16_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

Diagnostic Id:
CA1825: Avoid unnecessary zero-length array allocations. Use Array.Empty() instead.

Expected Behavior:
No suggestion.

Actual Behavior:
A suggestion is offered. We've increased the severity to Warning in Release builds. Combined with warnaserror, this breaks the build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions