Remove redundant 'ToCharArray' call rule + fixer#120
Merged
Conversation
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new GC2333 analyzer + code fixer that flags foreach loops iterating over string.ToCharArray() and rewrites them to iterate the string directly, eliminating the redundant char[] allocation. The PR also incidentally removes the <RestoreSources> element from two .csproj files.
Changes:
- New
RemoveRedundantToCharArrayCallanalyzer (onForEachStatement) and matchingRemoveRedundantToCharArrayCallFixer. - Registers the new rule id
GC2333inRule.Idsand adds a small test class with an empty-code case and a single fix scenario. - Drops
<RestoreSources>https://api.nuget.org/v3/index.json</RestoreSources>fromCreedengo.Tool.csprojandCreedengo.Package.csproj(not mentioned in the PR description).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Creedengo.Core/Analyzers/GC2333.RemoveRedundantToCharArrayCall.cs | New analyzer detecting foreach (… in <string>.ToCharArray()). |
| src/Creedengo.Core/Analyzers/GC2333.RemoveRedundantToCharArrayCall.Fixer.cs | New code fixer replacing the invocation with the string receiver. |
| src/Creedengo.Core/Models/Rule.cs | Registers the GC2333 rule id constant. |
| src/Creedengo.Tests/Tests/G2333.RemoveRedundantToCharArrayCall.Tests.cs | Adds happy-path + empty-code tests for the new rule. |
| src/Creedengo.Tool/Creedengo.Tool.csproj | Removes the <RestoreSources> line (unrelated to the rule). |
| src/Creedengo.Package/Creedengo.Package.csproj | Same <RestoreSources> removal as above. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <copilot@github.com>
vdebellabre
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: add GC2333 — Remove redundant
ToCharArraycallSummary
Adds a new analyzer and code fixer GC2333 that detects and removes redundant calls to
string.ToCharArray()inforeachloops.In C#, iterating over a
stringdirectly withforeachis equivalent to iterating overchar[]returned byToCharArray(). The extra call allocates an unnecessary array on the heap.Rule
Triggers on:
Fixed to:
Changes
foreachexpressions of the form<string>.ToCharArray()and verifies the method belongs toSystem.Stringvia the semantic modelGC2333.RemoveRedundantToCharArrayCall.Fixer.cs— code fixer: replaces the full invocation expression with the string instanceRule.cs— registers the new rule IDTests
All 240 tests pass.