-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add CA1830 analyzer for StringBuilder.Append(new string(char, int)) #51215
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
@copilot, you changed hundreds of files that didn't need to be changed as part of this PR. Please revert all of those (did you maybe run dotnet format and include all those changes accidentally)? |
Co-authored-by: stephentoub <[email protected]>
You're right, I accidentally included formatting changes from |
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 extends the existing CA1830 analyzer to detect and optimize a new inefficient StringBuilder pattern where new string(char, int)
constructor is used as an argument to StringBuilder.Append()
. The optimization suggests using StringBuilder.Append(char, int)
directly instead of creating an intermediate string object, improving performance by avoiding unnecessary allocations.
- Extends analyzer to detect
Append(new string(char, int))
pattern - Adds code fix to replace with direct
Append(char, int)
overload - Updates documentation and localization resources across multiple languages
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
PreferTypedStringBuilderAppendOverloadsTests.cs | Added comprehensive test cases for the new string constructor pattern detection |
PreferTypedStringBuilderAppendOverloads.cs | Extended analyzer logic to detect new string(char, int) in Append calls |
PreferTypedStringBuilderAppendOverloads.Fixer.cs | Added code fix to replace string constructor with direct Append overload |
MicrosoftNetCoreAnalyzersResources.resx | Updated analyzer description and added new code fix message |
Multiple .xlf files | Updated localization files with new resource strings |
Microsoft.CodeAnalysis.NetAnalyzers.sarif | Updated analyzer description in SARIF metadata |
Microsoft.CodeAnalysis.NetAnalyzers.md | Updated analyzer documentation |
Summary
This PR extends the existing CA1830 analyzer to detect and optimize an inefficient StringBuilder pattern where
new string(char, int)
is used as an argument toStringBuilder.Append()
.Changes Made
Analyzer
PreferTypedStringBuilderAppendOverloads.cs
to detectnew string(char, int)
constructor calls inStringBuilder.Append()
argumentsAppend
method (notInsert
) sinceInsert(int, char, int)
doesn't exist in StringBuilderCode Fix
PreferTypedStringBuilderAppendOverloads.Fixer.cs
to replaceAppend(new string(c, count))
withAppend(c, count)
Resources
PreferTypedStringBuilderAppendOverloadsReplaceStringConstructor
resource stringTests
Performance Impact
The suggested change avoids allocating an intermediate string object.
Before:
sb.Append(new string('*', 5))
- allocates stringAfter:
sb.Append('*', 5)
- no allocationFixes dotnet/runtime#117643
Original prompt
Fixes dotnet/runtime#117643
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.