Skip to content

Add analyzer for Enum.GetValues() with code fix to use generated method - #207

Merged
andrewlock merged 5 commits into
mainfrom
copilot/add-enum-getvalues-analyzer
Dec 28, 2025
Merged

Add analyzer for Enum.GetValues() with code fix to use generated method#207
andrewlock merged 5 commits into
mainfrom
copilot/add-enum-getvalues-analyzer

Conversation

Copilot AI commented Dec 24, 2025

Copy link
Copy Markdown
Contributor

Adds analyzer NEEG008 to detect Enum.GetValues() calls on enums with [EnumExtensions] and suggest the generated GetValues() method for better performance.

Implementation

  • GetValuesAnalyzer: Detects both Enum.GetValues(typeof(TEnum)) and Enum.GetValues<TEnum>() patterns, reporting as Information-level diagnostic
  • GetValuesCodeFixProvider: Replaces with non-extension static method ExtensionsClass.GetValues(), auto-adding namespace imports when needed
  • Tests: 15 test cases covering generic/non-generic variants, external enums, edge cases

Example

Before:

[EnumExtensions]
public enum Status { Active, Inactive }

var values = Enum.GetValues(typeof(Status));        // NEEG008
var values2 = Enum.GetValues<Status>();             // NEEG008

After applying fix:

var values = StatusExtensions.GetValues();
var values2 = StatusExtensions.GetValues();

Follows patterns from existing analyzers (ParseAnalyzer, ToStringAnalyzer, etc.).

Original prompt

This section details on the original issue you should resolve

<issue_title>Create analyzer to detect usages of Enum.GetValues() and suggest replacing with the generated GetValues() method</issue_title>
<issue_description>- Only enums which are decorated with [EnumExtensions] should be considered

  • The analyzer should be written using the same style as existing analyzers.
  • A code fixer should be provided, and should similarly have tests
  • The suggestion/fix should use the non-extension method GetValues implementation
  • All Enum.GetValues() overloads should be suggested for replacement.
  • Follow the patterns used by the ParseAnalyzer, ParseCodeFixProvider and ParseAnalyzerTests
  • The analyzer should be written as Information level</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Create analyzer for replacing Enum.GetValues() calls Add analyzer for Enum.GetValues() with code fix to use generated method Dec 24, 2025
Copilot AI requested a review from andrewlock December 24, 2025 22:51
@github-actions

github-actions Bot commented Dec 24, 2025

Copy link
Copy Markdown

Test Results

    180 files  ±  0      180 suites  ±0   3h 22m 54s ⏱️ + 49m 14s
 21 801 tests + 15   21 797 ✅ + 15   4 💤 ±0  0 ❌ ±0 
388 446 runs  +342  388 374 ✅ +342  72 💤 ±0  0 ❌ ±0 

Results for commit 4ca03ff. ± Comparison against base commit b3d1817.

♻️ This comment has been updated with latest results.

@andrewlock

Copy link
Copy Markdown
Owner

Updated to NEEG0009

@andrewlock
andrewlock force-pushed the copilot/add-enum-getvalues-analyzer branch from d68e8e3 to 4ca03ff Compare December 27, 2025 23:20
@andrewlock
andrewlock marked this pull request as ready for review December 27, 2025 23:20
@andrewlock
andrewlock merged commit ef1cc31 into main Dec 28, 2025
5 checks passed
@andrewlock
andrewlock deleted the copilot/add-enum-getvalues-analyzer branch December 28, 2025 14:39
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.

Create analyzer to detect usages of Enum.GetValues() and suggest replacing with the generated GetValues() method

2 participants