Conversation
Implements a new analyzer that detects unused internal classes, fixing known CA1812 false positives: - Correctly handles classes used as generic type arguments - Correctly handles classes referenced via typeof() in attributes The analyzer: - Tracks all internal classes in the compilation - Monitors their usage via object creation, generic type arguments, typeof operations, and array creation - Reports diagnostics for truly unused internal classes at compilation end Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
…ursion - Remove unused Compilation parameter from AnalyzerContext constructor - Prevent infinite recursion in AddUsedType by checking if type was already added before processing - Early return when type has already been processed Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
tests/Meziantou.Analyzer.Test/Rules/AvoidUnusedInternalTypesAnalyzerTests.cs
Show resolved
Hide resolved
tests/Meziantou.Analyzer.Test/Rules/AvoidUnusedInternalTypesAnalyzerTests.cs
Show resolved
Hide resolved
…ember references - Register handlers for PropertyReference, FieldReference, MethodReference, and EventReference operations - Track type arguments in the containing type when accessing members (e.g., Sample<InternalClass>.Empty) - Add test case for generic type with static member access using internal class as type argument Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
- Add test for XmlSerializer with internal class passed via typeof() - Add test for Newtonsoft.Json JsonConvert.DeserializeObject<T>() with internal class - Add test for YamlDotNet Deserialize<T>() with internal class - All tests verify that internal classes used by serializers are not incorrectly flagged as unused Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
|
@copilot Can you update |
|
@copilot I get a compilation error on CI Error: /home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Rules/AvoidUninstantiatedInternalClassesAnalyzer.cs(21,45): error CS0117: 'WellKnownDiagnosticTags' does not contain a definition for 'CompilationEnd' [/home/runner/work/Meziantou.Analyzer/Meziantou.Analyzer/src/Meziantou.Analyzer/Meziantou.Analyzer.csproj::TargetFramework=net10.0] |
docs/Rules/MA0182.md
Outdated
| @@ -0,0 +1,53 @@ | |||
| # MA0182 - Avoid uninstantiated internal classes | |||
|
|
|||
| This analyzer detects internal classes that are never instantiated or referenced. This is similar to CA1812 but fixes several known issues: | |||
Add MA0182 to the "Similar rules" section showing that it correctly handles internal classes used as generic type arguments and in typeof() expressions, addressing CA1812's known false positives. Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Updated |
| title: "Avoid uninstantiated internal classes", | ||
| messageFormat: "Internal class '{0}' 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'.", | ||
| RuleCategories.Design, | ||
| DiagnosticSeverity.Warning, |
- Remove WellKnownDiagnosticTags.CompilationEnd (doesn't exist) and use string array ["CompilationEnd"] instead - Change DiagnosticSeverity from Warning to Info (suggestion) as requested - Remove CA1812 mentions from rule documentation - Regenerate documentation files to reflect severity change Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Fixed the compilation error by replacing |
Implementation Plan: Fix CA1812 False Positives
JsonSerializer.Deserialize<InternalClass>())typeof()references in attributesSample<InternalClass>.Empty)Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.