Skip to content

CA1861 does not work with C#12 array syntax #7090

Open
@sungam3r

Description

@sungam3r

Analyzer

Diagnostic ID: CA1861: Avoid constant arrays as arguments

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 8.0.100

Describe the bug

CA1861 does not work with C#12 array syntax.

Steps To Reproduce

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <AnalysisMode>Recommended</AnalysisMode>
  </PropertyGroup>
</Project>
namespace ClassLibrary1
{
    public class Class1
    {
        public void Do()
        {
            DoSomething(new int[] { 1, 2 }); // CA1861
            DoSomething([1, 2]); // no CA1861
        }

        public static void DoSomething(int[] ints)
        {
        }
    }
}

Expected behavior

CA1861 on both lines

Actual behavior

CA1861 only for first line
изображение

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions