Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1812 generates false positive when class is created using collection initialization #7223

Open
RachelDavids opened this issue Mar 4, 2024 · 0 comments · May be fixed by #7229
Open

CA1812 generates false positive when class is created using collection initialization #7223

RachelDavids opened this issue Mar 4, 2024 · 0 comments · May be fixed by #7229

Comments

@RachelDavids
Copy link

RachelDavids commented Mar 4, 2024

Analyzer

CA1812

Analyzer source

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

Version: SDK 9.0.100-preview.1.24101.2

Describe the bug

Steps To Reproduce

    public class Class1
    {
        private readonly MyCollection _items = [];
        public int CurrentCount => _items.Count;
        public void AddItem(int value)
        {
            _items.Add(value);
        }
    }
    internal sealed class MyCollection
        : Collection<int>
    {

    }
    public class Class2
    {
#pragma warning disable IDE0028 // Simplify collection initialization
        private readonly MyCollection2 _items = new();
#pragma warning restore IDE0028 // Simplify collection initialization
    }
    internal sealed class MyCollection2
        : Collection<int>
    {

    }

Expected behavior

No warning/error

Actual behavior

  Error CA1812: 'MyCollection' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic). (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1812) (14, 27)

Additional context

Using SDK 8.0.200 produces the same behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant