Skip to content

Detect uninstantiated classes used in XXXSource attributes - #1012

Open
manfred-brands wants to merge 5 commits into
nunit:masterfrom
manfred-brands:Issue999_TestSourceClasses
Open

Detect uninstantiated classes used in XXXSource attributes#1012
manfred-brands wants to merge 5 commits into
nunit:masterfrom
manfred-brands:Issue999_TestSourceClasses

Conversation

@manfred-brands

Copy link
Copy Markdown
Member

Fixes #999

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds additional CA1812 suppression coverage so internal classes that are instantiated indirectly by NUnit (via *Source attributes) don’t get flagged as “uninstantiated”.

Changes:

  • Extend the CA1812 suppressor to attempt to detect classes referenced from *Source attributes (e.g., TestCaseSource).
  • Add a suppressor test covering a class used via [TestCaseSource(typeof(...))].
  • Update NUnit3003 documentation title/summary text to mention test data sources.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/nunit.analyzers/DiagnosticSuppressors/AvoidUninstantiatedInternalClassSuppressor.cs Expands suppression logic beyond TestFixture to include *Source-referenced classes.
src/nunit.analyzers.tests/DiagnosticSuppressors/AvoidUninstantiatedInternalClassesSuppressorTests.cs Adds a unit test for the new suppression scenario.
documentation/NUnit3003.md Updates rule title/config snippet text to include “TestData source”.
documentation/index.md Updates the index entry text for NUnit3003.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread documentation/NUnit3003.md

@mikkelbu mikkelbu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @manfred-brands. What do you think about the comment from Copilot about the check uses the syntax as a string rather than using the semantic model?

We can also complete this as a first step and then use the semantic model in a subsequent enhancement?

I've approved this, but I would like to hear your thoughts about this as I guess you have thought more about this than I have.

@manfred-brands
manfred-brands marked this pull request as draft July 10, 2026 01:34
@manfred-brands

Copy link
Copy Markdown
Member Author

Sorry @mikkelbu, I should have put this in Draft. I forgot and didn't had a chance to look into the alternative approach. I should have time this weekend.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

@manfred-brands
manfred-brands marked this pull request as ready for review July 11, 2026 07:00
@manfred-brands
manfred-brands requested a review from mikkelbu July 11, 2026 07:00
@manfred-brands

Copy link
Copy Markdown
Member Author

@mikkelbu I have reworked to suppressor to use the SyntaxTree instead of string and also optimized it to only loop over all source trees once. As this is still expensive and a niche operation, I put it behind a configuration option.

Most use case would use static members of a class and that class should be declared static instead of suppressing the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TestCaseSource] CA1812 suppression

3 participants