Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 6, 2026

StringBuilder.Append(object?) implicitly calls ToString() on enum values, which is significantly slower than the generated ToStringFast() method. This analyzer detects such usage and suggests the optimized alternative.

Implementation

  • StringBuilderAppendAnalyzer: Diagnostic NEEG012 (Warning) that flags:
    • StringBuilder.Append(enumValue) calls where the enum has [EnumExtensions] or is in [EnumExtensions<T>]
  • StringBuilderAppendCodeFixProvider: Automatic fix replaces enum arguments with enumValue.ToStringFast()
  • Tests: 19 test cases covering standard usage, external enums, namespace handling, and edge cases

Examples

StringBuilder.Append

var sb = new StringBuilder();
MyEnum value = MyEnum.First;

// Flagged by NEEG012
sb.Append(value);

// Suggested fix
sb.Append(value.ToStringFast());

Follows the pattern established by ToStringAnalyzer (NEEG004) for string interpolation detection.

Original prompt

This section details on the original issue you should resolve

<issue_title>Detect usage of StringBuilder.Append() and encourage replacing with ToStringFast()</issue_title>
<issue_description>Original request:

Hello, @andrewlock
i thing, analyzer can check use enum members in StringBuilder.Append(object? value) method, because Append(object? value) invoke ToStirng()method of object

Originally posted by @tecAmoRaller in #195
</issue_description>

<agent_instructions>We're looking for code that looks like this:

var sb = new StringBuilder();
MyEnum value = MyEnum.First;
sb.Append(value);

and suggesting replacing with:

var sb = new StringBuilder();
MyEnum value = MyEnum.First;
sb.Append(value.ToStringFast());
  • Only enums which are decorated with [EnumExtensions] or referenced in [EnumExtensions<T> should be considered
  • The analyzer should be written using the same style as existing analyzers, for example the ToStringAnalyzer.
  • A code fixer should be provided, and should similarly have tests that match the existing tests for other analyzers
  • The analyzer should be written as Warning level
  • The suggestion/fix should use the extension implementation of ToStringFast()</agent_instructions>

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.

Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copilot AI changed the title [WIP] Encourage replacing StringBuilder.Append with ToStringFast Add analyzer for StringBuilder.Append(enum) to suggest ToStringFast() (NEEG012) Jan 6, 2026
Copilot AI requested a review from andrewlock January 6, 2026 19:48
Copy link
Owner

@andrewlock andrewlock left a 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

Copilot AI and others added 2 commits January 8, 2026 22:26
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Co-authored-by: andrewlock <18755388+andrewlock@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 8, 2026

@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 s...

Added support for StringBuilder.AppendFormat() in commit 6548e92. The analyzer now detects enum arguments in all AppendFormat overloads (with and without IFormatProvider). Added 9 comprehensive tests covering various scenarios including multiple enums, mixed argument types, and provider overloads. All 28 tests passing.

Copilot AI changed the title Add analyzer for StringBuilder.Append(enum) to suggest ToStringFast() (NEEG012) Add analyzer for StringBuilder.Append/AppendFormat(enum) to suggest ToStringFast() (NEEG012) Jan 8, 2026
Copilot AI requested a review from andrewlock January 8, 2026 22:31
Copy link
Owner

@andrewlock andrewlock left a 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

@andrewlock andrewlock marked this pull request as ready for review January 8, 2026 22:45
@andrewlock andrewlock requested a review from Copilot January 8, 2026 22:54
@github-actions
Copy link

github-actions bot commented Jan 8, 2026

Test Results

    180 files  ±  0      180 suites  ±0   6h 37m 29s ⏱️ + 1h 58m 19s
 21 876 tests + 19   21 872 ✅ + 19   4 💤 ±0  0 ❌ ±0 
389 982 runs  +414  389 910 ✅ +414  72 💤 ±0  0 ❌ ±0 

Results for commit 90bbfe4. ± Comparison against base commit 890f053.

Copy link
Contributor

Copilot AI left a 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 StringBuilderAppendAnalyzer to detect StringBuilder.Append(enumValue) calls
  • Provides StringBuilderAppendCodeFixProvider to automatically replace with ToStringFast()
  • 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.

return;
}


Copy link

Copilot AI Jan 8, 2026

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.

Suggested change

Copilot uses AI. Check for mistakes.
@andrewlock andrewlock changed the title Add analyzer for StringBuilder.Append/AppendFormat(enum) to suggest ToStringFast() (NEEG012) Add analyzer for StringBuilder.Append(enum) to suggest ToStringFast() (NEEG012) Jan 8, 2026
@andrewlock andrewlock merged commit e3d4981 into main Jan 8, 2026
11 checks passed
@andrewlock andrewlock deleted the copilot/detect-stringbuilder-append-usage branch January 9, 2026 19:29
@tecAmoRaller
Copy link

@andrewlock plz publish new version

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.

Detect usage of StringBuilder.Append() and encourage replacing with ToStringFast()

3 participants