Skip to content

Add analyzer to warn when enums are nested inside generic types#157

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/fix-115-2
Closed

Add analyzer to warn when enums are nested inside generic types#157
Copilot wants to merge 1 commit intomainfrom
copilot/fix-115-2

Conversation

Copy link
Contributor

Copilot AI commented Jun 23, 2025

This PR adds a new analyzer (diagnostic NEEG002) that detects when an enum decorated with [EnumExtensions] is nested inside a generic parent type and generates a warning, since this scenario is not currently supported.

Problem

Previously, the generator would attempt to generate extension methods for enums nested inside generic types, which would result in invalid code generation. For example:

public class GenericContainer<T>
{
    [EnumExtensions]
    public enum MyEnum  // This would generate invalid code
    {
        First,
        Second,
    }
}

Solution

The implementation adds:

  1. New diagnostic descriptor EnumNestedInGenericType (NEEG002) with clear warning message
  2. Hierarchy detection logic that walks up the containment chain to check if any parent type is generic
  3. Enhanced generation pipeline that can handle both successful generation and diagnostic reporting
  4. Comprehensive test coverage including deeply nested scenarios

Implementation Details

  • Created EnumGenerationResult struct to handle both success and diagnostic cases
  • Added GetEnumGenerationResult method that checks the type hierarchy before attempting generation
  • When a generic parent is detected, reports the diagnostic and skips code generation
  • Updated the source generation pipeline with ExecuteWithResult to handle diagnostics

Verification

The implementation correctly handles all scenarios:

  • ✅ Enum directly nested in generic class → generates warning
  • ✅ Enum in deeply nested hierarchy with generic ancestor → generates warning
  • ✅ Enum in non-generic nested class → works normally
  • ✅ Top-level enum → works normally

Fixes #115.


💡 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.

@andrewlock andrewlock closed this Jun 23, 2025
@andrewlock andrewlock deleted the copilot/fix-115-2 branch June 23, 2025 18:59
Copilot AI changed the title [WIP] Handle enums that are nested inside generic types Add analyzer to warn when enums are nested inside generic types Jun 23, 2025
Copilot AI requested a review from andrewlock June 23, 2025 19:10
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.

Handle enums that are nested inside generic types

2 participants