Skip to content

Add analyzer warning for duplicate enum case labels (NEEG003)#162

Merged
andrewlock merged 7 commits intomainfrom
copilot/fix-161
Jul 19, 2025
Merged

Add analyzer warning for duplicate enum case labels (NEEG003)#162
andrewlock merged 7 commits intomainfrom
copilot/fix-161

Conversation

Copy link
Contributor

Copilot AI commented Jul 18, 2025

Adds a new analyzer DuplicateEnumValueAnalyzer that warns developers when enum members have duplicate values and will be excluded from generated switch statements.

Background

Issue #66 highlighted that the generator creates compiler warnings when generating ToStringFast() methods for enums with duplicate values. PR #143 addressed this by using a HashSet<object> to track enum values and only creating switch cases for the first occurrence of each value. However, this means that duplicate enum members are silently excluded from the generated code, which can be confusing for developers.

Changes

  • New Analyzer: DuplicateEnumValueAnalyzer (diagnostic ID: NEEG003)
  • Scope: Only analyzes enums decorated with [EnumExtensions] attribute
  • Severity: DiagnosticSeverity.Info to provide helpful warnings without breaking builds
  • Message: Clear explanation that the enum member will be excluded from generated switch statements

Example

[EnumExtensions]
public enum Status
{
    Unknown = 0,
    Pending = 1,
    Failed = 2,
    Error = 2,  // ⚠️ NEEG003: Will be excluded from generated switch statements
}

The analyzer will warn about Error since it has the same value as Failed and will not be included in the generated ToStringFast(), IsDefined(), and other switch-based methods.

Test Coverage

Added comprehensive test suite with 9 test cases covering:

  • Enums without [EnumExtensions] attribute (ignored)
  • Simple and complex duplicate scenarios
  • Implicit enum values
  • Different enum underlying types
  • Multiple enums with separate tracking

Validation

  • ✅ All 127 tests passing (9 new + 118 existing)
  • ✅ No regressions in existing functionality
  • ✅ Follows existing analyzer patterns in the codebase
  • ✅ Manual testing with sample projects

This provides developers with early visibility into which enum members will be excluded from generated code, complementing the fix in PR #143.

Fixes #161.


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

Copilot AI and others added 3 commits July 18, 2025 21:33
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
…ation

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot AI changed the title [WIP] Add analyzer warning for duplicate case label Add analyzer warning for duplicate enum case labels (NEEG003) Jul 18, 2025
Copilot AI requested a review from andrewlock July 18, 2025 21:47
@github-actions
Copy link

github-actions bot commented Jul 18, 2025

Test Results

   144 files  ±    0     144 suites  ±0   40m 17s ⏱️ + 37m 45s
 5 382 tests +   10   5 378 ✅ +   10   4 💤 ±0  0 ❌ ±0 
94 780 runs  +2 186  94 708 ✅ +2 186  72 💤 ±0  0 ❌ ±0 

Results for commit 0e876db. ± Comparison against base commit 360a0d0.

♻️ This comment has been updated with latest results.

Copy link
Owner

@andrewlock andrewlock left a comment

Choose a reason for hiding this comment

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

LGTM after tweaks

@andrewlock andrewlock marked this pull request as ready for review July 19, 2025 13:57
@andrewlock andrewlock merged commit d3fa4a8 into main Jul 19, 2025
5 checks passed
@andrewlock andrewlock deleted the copilot/fix-161 branch July 19, 2025 13:58
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.

Add analyzer warning for duplicate case label

2 participants