Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Implements analyzer and code fix to warn when metadata attributes ([Display], [Description], [EnumMember]) will be ignored due to mismatched MetadataSource configuration.

Changes

Analyzer (NEEG004)

  • Reports Info diagnostic when enum members use attributes incompatible with configured MetadataSource
  • Respects explicit MetadataSource in [EnumExtensions], falls back to EnumGenerator_EnumMetadataSource MSBuild property, defaults to EnumMemberAttribute
  • Only warns when no compatible attributes exist (prevents noise in mixed-attribute scenarios)
  • Skips enums with MetadataSource.None

Code Fix Provider

  • Infers correct MetadataSource from present attributes
  • Adds or updates MetadataSource property while preserving other attribute arguments

Example

// Before (triggers NEEG004)
[EnumExtensions]  // defaults to EnumMemberAttribute
public enum Status
{
    [Display(Name = "Active")]  // ⚠️ Ignored - Display incompatible with EnumMemberAttribute
    Active,
}

// After applying code fix
[EnumExtensions(MetadataSource = MetadataSource.DisplayAttribute)]
public enum Status
{
    [Display(Name = "Active")]  // ✓ Now used for ToStringFast()
    Active,
}

Test Coverage

  • 12 new test cases covering explicit/global/default configuration scenarios
  • 3 tests skipped pending MSBuild property test infrastructure
Original prompt

This section details on the original issue you should resolve

<issue_title>Add analyzer warning about using the wrong metadata types.</issue_title>
<issue_description>We should consider adding an analyzer and code fix that adds an informational diagnostic when you're using metadata attributes that will be ignored due to the current selected MetadataSource. We could also add a code fix that automatically sets the "correct" value in the MetadataSource.


Background

In #163 we added support for [EnumMember]. This also changed the behaviour of metadata attributes to only allow using a single metadata attribute, i.e. either [EnumMember], [Display] or [Description]. You select the metadata type by either setting the MetadataSource property in the [EnumExtensions] attribute:

[EnumExtensions(MetadataSource = MetadataSource.DisplayAttribute)]
public enum EnumWithDisplayNameInNamespace
{
    First = 0,
    [Display(Name = "2nd")]
    Second = 1,
    Third = 2,
}

Or by setting it globally as a fallback:

<PropertyGroup>
  <EnumGenerator_EnumMetadataSource>DisplayAttribute</EnumGenerator_EnumMetadataSource>
</PropertyGroup>

and finally we fallback to [EnumMember]

Required Analyzer behaviour

The analyzer should identify cases where an enum decorated with [EnumExtensions] (which hasn't specified MetadataSource) has members that are using metadata attributes which will be ignored. For example the following would be identified, because the default source is [EnumMember], but this uses [Display]:

[EnumExtensions]
public enum EnumWithDisplayNameInNamespace
{
    First = 0,
    [Display(Name = "2nd")]
    Second = 1,
    Third = 2,
}

If the user had specified [EnumMember] anywhere in this type, the [Display] attribute would not be flagged.

Note that the "default" value must be calculated using the fallback MSBuild variable.

Required Code fix behaviour

For cases we identify, we should provide a code fix that automatically adds the appropriate MetadataSource property value to the [EnumExtensions] attribute for the type, or allows setting the value globally as an MSBuild property.

Implementation

Follow the existing analyzer examples. Make sure to add unit tests to NetEscapades.EnumGenerators.Tests to confirm all the expected behaviours, using the same testing helpers already used in the project. </issue_description>

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


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

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot AI changed the title [WIP] Add analyzer warning for incorrect metadata types Add analyzer for ignored metadata attributes (NEEG004) Nov 18, 2025
Copilot AI requested a review from andrewlock November 18, 2025 22:27
Copilot finished work on behalf of andrewlock November 18, 2025 22:27
@github-actions
Copy link

github-actions bot commented Nov 19, 2025

Test Results

    80 files   -     64      80 suites   - 64   22m 28s ⏱️ - 19m 31s
 6 768 tests +    15   6 759 ✅ +    12   9 💤 + 3  0 ❌ ±0 
67 680 runs   - 51 218  67 590 ✅  - 51 200  90 💤  - 18  0 ❌ ±0 

Results for commit 2131123. ± Comparison against base commit 0e5f4b5.

♻️ This comment has been updated with latest results.

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 about using the wrong metadata types.

2 participants