-
Notifications
You must be signed in to change notification settings - Fork 53
Add analyzer for StringBuilder.Append(enum) to suggest ToStringFast() (NEEG012) #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
andrewlock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot we should probably also handle the various StringBuilder.AppendFormat() methods too, where the first argument is a string and one of the other arguments is a target enum. We can use the same analyzer for this I think
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Added support for |
andrewlock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Append format is actually way more complicated so it's probably not worth the effort
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new analyzer (NEEG012) that detects when enum values are passed directly to StringBuilder.Append(object?), which implicitly calls ToString(). The analyzer suggests using the generated ToStringFast() method instead for better performance.
Key changes:
- Implements
StringBuilderAppendAnalyzerto detectStringBuilder.Append(enumValue)calls - Provides
StringBuilderAppendCodeFixProviderto automatically replace withToStringFast() - Adds comprehensive test coverage with 19 test cases
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/StringBuilderAppendAnalyzer.cs |
New analyzer that flags StringBuilder.Append() calls with enum arguments |
src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/StringBuilderAppendCodeFixProvider.cs |
Code fix provider that wraps enum arguments with ToStringFast() |
tests/NetEscapades.EnumGenerators.Tests/StringBuilderAppendAnalyzerTests.cs |
19 test cases covering various scenarios including external enums and namespace handling |
tests/NetEscapades.EnumGenerators.IntegrationTests/AnalyzerTests.cs |
Integration test for NEEG012 with pragma disable/restore |
tests/NetEscapades.EnumGenerators.Nuget.AnalyzerTests/AnalyzerTests.cs |
NuGet package analyzer test case for NEEG012 |
tests/NetEscapades.EnumGenerators.IntegrationTests/.editorconfig |
Adds NEEG012 severity configuration |
README.md |
Documents NEEG012 in the analyzer configuration section |
src/NetEscapades.EnumGenerators/README.md |
Documents NEEG012 in the package README |
build/Build.cs |
Adds NEEG012 to expected analyzer errors in build verification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/NetEscapades.EnumGenerators/Diagnostics/UsageAnalyzers/StringBuilderAppendAnalyzer.cs
Show resolved
Hide resolved
| return; | ||
| } | ||
|
|
||
|
|
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blank line can be removed for consistency with other analyzers in the codebase.
|
@andrewlock plz publish new version |
StringBuilder.Append(object?)implicitly callsToString()on enum values, which is significantly slower than the generatedToStringFast()method. This analyzer detects such usage and suggests the optimized alternative.Implementation
StringBuilder.Append(enumValue)calls where the enum has[EnumExtensions]or is in[EnumExtensions<T>]enumValue.ToStringFast()Examples
StringBuilder.Append
Follows the pattern established by ToStringAnalyzer (NEEG004) for string interpolation detection.
Original prompt
StringBuilder.Append()and encourage replacing withToStringFast()#229💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.