Skip to content

Add NEEG004 analyzer to detect ToString() on enums with [EnumExtensions] or EnumExtensions<T> - #196

Merged
andrewlock merged 11 commits into
mainfrom
copilot/add-analyzer-for-tostring-replacement
Dec 17, 2025
Merged

Add NEEG004 analyzer to detect ToString() on enums with [EnumExtensions] or EnumExtensions<T>#196
andrewlock merged 11 commits into
mainfrom
copilot/add-analyzer-for-tostring-replacement

Conversation

Copilot AI commented Dec 15, 2025

Copy link
Copy Markdown
Contributor

Implements analyzer to detect ToString() calls on enums decorated with [EnumExtensions] or referenced in EnumExtensions<T> assembly attributes and suggest ToStringFast() replacement for better performance.

Changes

  • ToStringAnalyzer (NEEG004): Information-level diagnostic for ToString() calls (with or without format specifiers) on enums with [EnumExtensions] attribute or external enums referenced in EnumExtensions<T> assembly attributes
    • Handles format specifiers: only suggests replacement for compatible formats (empty string, "G", "g")
    • Does not flag incompatible format specifiers like "x", "X", "d", "D"
    • Detects external enum types declared via [assembly: EnumExtensions<T>()] attributes (e.g., DateTimeKind, FileShare)
  • ToStringCodeFixProvider: Batch fixer to replace ToString()ToStringFast() with trivia preservation
    • Removes format parameters when replacing with ToStringFast()
  • Test coverage: 32 test cases covering various contexts (expressions, return statements, multiple enums, attribute configurations, format specifiers, external enum types)

Example

// Direct attribute on enum
[EnumExtensions]
public enum Status { Active, Inactive }

// External enum via assembly attribute
[assembly: EnumExtensions<System.DateTimeKind>()]

// Before: NEEG004 info diagnostic
var text1 = status.ToString();
var text2 = status.ToString("G");
var text3 = dateTimeKind.ToString();

// After fix applied
var text1 = status.ToStringFast();
var text2 = status.ToStringFast();
var text3 = dateTimeKind.ToStringFast();

// Not flagged (incompatible format specifiers)
var hex = status.ToString("x");  // No diagnostic

Analyzer triggers on:

  • Enums with [EnumExtensions] attribute
  • External enums referenced in EnumExtensions<T> assembly attributes
  • Only for compatible format specifiers
Original prompt

This section details on the original issue you should resolve

<issue_title>Create analyzer to detect usages of ToString() and suggest replacing with ToStringFast()</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 analyzer should be written as Information level
    </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.

Copilot AI self-assigned this Dec 15, 2025
@github-actions

github-actions Bot commented Dec 15, 2025

Copy link
Copy Markdown

Test Results

    180 files  ±  0      180 suites  ±0   1h 8m 45s ⏱️ + 19m 40s
 21 694 tests + 33   21 690 ✅ + 33   4 💤 ±0  0 ❌ ±0 
386 160 runs  +666  386 088 ✅ +666  72 💤 ±0  0 ❌ ±0 

Results for commit 48573cd. ± Comparison against base commit aabb4d1.

♻️ This comment has been updated with latest results.

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot AI changed the title [WIP] Create analyzer to detect usages of ToString() and suggest ToStringFast() Add NEEG004 analyzer to detect ToString() on enums with [EnumExtensions] Dec 15, 2025
Copilot AI requested a review from andrewlock December 15, 2025 22:34
Comment thread src/NetEscapades.EnumGenerators/Diagnostics/ToStringAnalyzer.cs Outdated
…nt for compatible formats (G, g, empty string)

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
@andrewlock
andrewlock marked this pull request as ready for review December 17, 2025 19:47

#pragma warning disable NEEG003

[assembly:NetEscapades.EnumGenerators.EnumExtensions<DateTimeKind>()]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot when checking if an enum is valid to be replaced with ToStringFast(), we need to check if it's referenced in an EnumExtensions<T> attribute as well. Add tests and update the implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in commit b291228. The analyzer now checks for EnumExtensions<T> assembly attributes and collects all external enum types during compilation start. Added 8 new tests covering external enum scenarios (DateTimeKind, FileShare) with various format specifiers. All 198 tests pass.

…Extensions<T> attributes

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot AI changed the title Add NEEG004 analyzer to detect ToString() on enums with [EnumExtensions] Add NEEG004 analyzer to detect ToString() on enums with [EnumExtensions] or EnumExtensions<T> Dec 17, 2025
Copilot AI requested a review from andrewlock December 17, 2025 20:07
@andrewlock
andrewlock merged commit 4c4279d into main Dec 17, 2025
5 checks passed
@andrewlock
andrewlock deleted the copilot/add-analyzer-for-tostring-replacement branch December 17, 2025 21:06
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 ToString() and suggest replacing with ToStringFast()

2 participants