Skip to content

[TestCaseSource] CA1812 suppressionΒ #999

Closed

Description

@uladz-zubrycki

Hello,

I'd like to suggest an idea that is similar to the #565, but for the types that are indirectly instantiated with the use of a NUnit's TestCaseSourceAttribute.

If an attribute-based instantiation is the only for the type, it fires CA1812, which is a false positive. Having a suppression for such cases shipped in the NUnit.Analyzers would be useful.

Thanks.

Activity

  1. manfred-brands commented on Jun 26, 2026

    @manfred-brands
    Member

    @uladz-zubrycki Could you add an minimal example please?

    Are you using nameof(source) for the TestCaseSource. That would be a usage of the member which then should be a usage of the type.

  2. uladz-zubrycki commented on Jun 26, 2026

    @uladz-zubrycki
    Author

    @manfred-brands, Sure.

    dotnet_diagnostic.CA1812.severity = warning
    
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    
    internal sealed class FooSource: IEnumerable
    {
    	public IEnumerator GetEnumerator() => throw new NotImplementedException();
    }
    
    [TestFixture]
    internal sealed class FooFixture
    {
    	[TestCaseSource(typeof(FooSource))]
    	public void Foo() { }
    }
    

    Produces

    Error CA1812 : 'FooSource' 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)
    

    That would be a usage of the member which then should be a usage of the type.

    CA1812 is detecting types that are never instantiated, not unused types.

  3. manfred-brands commented on Jul 3, 2026

    @manfred-brands
    Member

    @uladz-zubrycki This one is problematic.

    The analyzer reports the 'uninstantiated' class at its declaration location, which could be in a different file all together.
    It could be referenced multiple times anywhere.

    I have found one way where I search all sources that are part of the compilation for the text: Source(typeof(XXX) where XXX is the class reported to be uninstantiated.
    This detects all 3 NUnit attributes: TestCaseSource, ValueSource and TestFixtureSource.

  4. added this to the Release 4.15 milestone on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions