Skip to content

Remove redundant 'ToCharArray' call rule + fixer#120

Merged
vdebellabre merged 3 commits into
green-code-initiative:mainfrom
PendingChanges:main
May 19, 2026
Merged

Remove redundant 'ToCharArray' call rule + fixer#120
vdebellabre merged 3 commits into
green-code-initiative:mainfrom
PendingChanges:main

Conversation

@PendingChanges
Copy link
Copy Markdown
Contributor

feat: add GC2333 — Remove redundant ToCharArray call

Summary

Adds a new analyzer and code fixer GC2333 that detects and removes redundant calls to string.ToCharArray() in foreach loops.

In C#, iterating over a string directly with foreach is equivalent to iterating over char[] returned by ToCharArray(). The extra call allocates an unnecessary array on the heap.

Rule

ID GC2333
Category Performance
Severity Warning

Triggers on:

foreach (char c in s.ToCharArray())  // ⚠ GC2333
    Console.WriteLine(c);

Fixed to:

foreach (char c in s)
    Console.WriteLine(c);

Changes

  • GC2333.RemoveRedundantToCharArrayCall.cs — analyzer: detects foreach expressions of the form <string>.ToCharArray() and verifies the method belongs to System.String via the semantic model
  • GC2333.RemoveRedundantToCharArrayCall.Fixer.cs — code fixer: replaces the full invocation expression with the string instance
  • Rule.cs — registers the new rule ID
  • G2333.RemoveRedundantToCharArrayCall.Tests.cs — tests: empty-code no-op + fix scenario

Tests

All 240 tests pass.

Copy link
Copy Markdown

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

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 RemoveRedundantToCharArrayCall analyzer (on ForEachStatement) and matching RemoveRedundantToCharArrayCallFixer.
  • Registers the new rule id GC2333 in Rule.Ids and 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> from Creedengo.Tool.csproj and Creedengo.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.

Comment thread src/Creedengo.Core/Models/Rule.cs Outdated
Comment thread src/Creedengo.Tool/Creedengo.Tool.csproj Outdated
Co-authored-by: Copilot <copilot@github.com>
@vdebellabre vdebellabre merged commit 5116a48 into green-code-initiative:main May 19, 2026
1 check passed
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.

3 participants