Skip to content

CA1861 exclude static ReadOnlyCollection<T> field/properties #6629

@buyaa-n

Description

@buyaa-n

Analyzer CA1861 Avoid constant arrays as arguments

Diagnostic ID: CA1861

Describe the improvement

We have excluded static readonly fields/properties instantiation, we also need to exclude static ReadOnlyCollection<T> field/property initializtions

Example:

public class Test
{
    private static ReadOnlyCollection<string>? s_errorPayloadNames;

    private void M(string eventName, string msg)
    {
        var eventCallbackArgs = new EventWrittenEventArgs(this, 0)
        {
            EventName = eventName,
            Message = msg,
            Payload = new ReadOnlyCollection<object?>(new object[] { msg }),
            // Should not warn here
            PayloadNames = s_errorPayloadNames ??= new ReadOnlyCollection<string>(new string[] { "message" })
        };
    }
}

Additional context

Related to dotnet/runtime#86229 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions