You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
Activity
manfred-brands commentedon Jun 26, 2026
@uladz-zubrycki Could you add an minimal example please?
Are you using
nameof(source)for theTestCaseSource. That would be a usage of the member which then should be a usage of the type.uladz-zubrycki commentedon Jun 26, 2026
@manfred-brands, Sure.
Produces
CA1812 is detecting types that are never instantiated, not unused types.
manfred-brands commentedon Jul 3, 2026
@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,ValueSourceandTestFixtureSource.