Skip to content

[11.0-preview.1] CA1825 false positives when not targeting net11.0 #53047

@martincostello

Description

@martincostello

Describe the bug

When collection expressions are used in a project which multi-targets to include TFMs other than net11.0, false positive CA1825 warnings are raised for code patterns that produced no warnings using the .NET 10 SDK.

❯ dotnet build --configuration Release
Restore complete (0.4s)
    info NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  UriRegressionRepro net462 succeeded (0.3s) → bin\Release\net462\UriRegressionRepro.dll
  UriRegressionRepro net10.0 succeeded with 2 warning(s) (0.3s) → bin\Release\net10.0\UriRegressionRepro.dll
    D:\Coding\_repros\ReproTests.cs(6,5): warning CA1825: Avoid unnecessary zero-length array allocations.  Use Array.Empty<int>() instead. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1825)
    D:\Coding\_repros\ReproTests.cs(21,38): warning CA1825: Avoid unnecessary zero-length array allocations.  Use Array.Empty<int>() instead. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1825)
  UriRegressionRepro net11.0 succeeded (0.5s) → bin\Release\net11.0\UriRegressionRepro.dll

Build succeeded with 2 warning(s) in 1.2s

The only way, other than suppressing the warnings, to get code that works for all TFMs appears to be to not use collection expressions at all.

To Reproduce

namespace UriRegressionRepro;

public class ZeroLengthAllocationsRepro
{
    public static TheoryData<int> IntegersProperty =>
    [
        1,
        2,      
    ];

    public static TheoryData<int> IntegersMethod()
    {
        TheoryData<int> theoryData = [];

        for (int i = 1; i <= 10; i++)
        {
            theoryData.Add(i);
        }

        return theoryData;
    }

    [Theory]
    [MemberData(nameof(IntegersProperty))]
    public void Twice(int value)
    {
        Assert.Equal(value * 2, value + value);
    }

    [Theory]
    [MemberData(nameof(IntegersMethod))]
    public void Thrice(int value)
    {
        Assert.Equal(value * 3, value + value + value);
    }
}
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <AnalysisMode>All</AnalysisMode>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>latest</LangVersion>
    <TargetFrameworks>net11.0;net10.0;net462</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
    <PackageReference Include="xunit" Version="2.9.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
  </ItemGroup>
  <ItemGroup>
    <Using Include="Xunit" />
  </ItemGroup>
</Project>

Exceptions (if any)

None.

Further technical details

.NET SDK 11.0.100-preview.1.26104.118

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