Skip to content

Add CA1812 suppressor for internal classes used as type parameters#116

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/suppress-ca1812-warnings
Draft

Add CA1812 suppressor for internal classes used as type parameters#116
Copilot wants to merge 4 commits intomasterfrom
copilot/suppress-ca1812-warnings

Conversation

Copy link
Contributor

Copilot AI commented Nov 30, 2025

CA1812 ("Avoid uninstantiated internal classes") produces false positives when internal classes are instantiated via reflection or external code (e.g., JSON deserializers, DI containers). This adds a DiagnosticSuppressor to suppress CA1812 when the class is referenced as a type argument.

Changes

  • CA1812Suppressor.cs: New suppressor (RGS001) that scans for type argument usage in generic invocations, generic types, and typeof expressions
  • CA1812SuppressorTests.cs: Unit tests for suppressor descriptor properties and integration tests that verify actual CA1812 suppression behavior using a mock analyzer
  • README.md: Documentation under new "Suppressions" section

Example

internal class Model { // CA1812 suppressed
    public int Id { get; set; }
}

Model model = JsonConvert.DeserializeObject<Model>(json); // Type argument triggers suppression
Type t = typeof(Model); // Also triggers suppression
Original prompt

This section details on the original issue you should resolve

<issue_title>Suppress CA1812 (internal class never instantiated) if class is referenced as type parameter</issue_title>
<issue_description>```cs
internal class Model { // CA 1812
public int Id { get; set; }
}

Model model = JsonConvert.DeserializeObject(); // suppress CA1812


## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@ronnygunawan</author><body>
You can suppress warnings from other analyzer by using the `DiagnosticSuppressor`</body></comment_new>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 30, 2025 01:45
Co-authored-by: ronnygunawan <3048897+ronnygunawan@users.noreply.github.com>
Co-authored-by: ronnygunawan <3048897+ronnygunawan@users.noreply.github.com>
Copilot AI changed the title [WIP] Suppress CA1812 warning for referenced type parameters Add CA1812 suppressor for internal classes used as type parameters Nov 30, 2025
Copilot AI requested a review from ronnygunawan November 30, 2025 01:51
Copy link
Owner

@ronnygunawan ronnygunawan left a comment

Choose a reason for hiding this comment

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

Add test cases where this suppressor suppresses an actual warning

…nostics

Co-authored-by: ronnygunawan <3048897+ronnygunawan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 30, 2025

Add test cases where this suppressor suppresses an actual warning

Added integration tests using a mock CA1812 analyzer that verifies actual suppression behavior. The new tests cover:

  • InternalClassUsedAsGenericTypeArgument_CA1812IsSuppressed - Tests List<Model>
  • InternalClassUsedInGenericMethodCall_CA1812IsSuppressed - Tests Deserialize<Model>()
  • InternalClassUsedInTypeOf_CA1812IsSuppressed - Tests typeof(Model)
  • InternalClassNotUsedAsTypeArgument_CA1812IsNotSuppressed - Verifies warning is NOT suppressed when class isn't used as type argument

Commit: a0a3f09

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.

Suppress CA1812 (internal class never instantiated) if class is referenced as type parameter

2 participants