NetEscapades.EnumGenerators icon indicating copy to clipboard operation
NetEscapades.EnumGenerators copied to clipboard

Add analyzer for ignored metadata attributes (NEEG004)

Open Copilot opened this issue 3 months ago • 1 comments

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 andrewlock/NetEscapades.EnumGenerators#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)

  • Fixes andrewlock/NetEscapades.EnumGenerators#173

💡 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 avatar Nov 18 '25 22:11 Copilot