Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 31, 2026

Description

string.Format("message") with no format arguments is a no-op. This PR removes the unnecessary wrapper, replacing it with just the string literal.

Similar to #123834 which addressed SR.Format calls.

Changes

  • 239 replacements across 18 files
  • Primarily test files in System.Runtime, plus GCLogParser tool

Example

// Before
Assert.Fail(string.Format("Expected exception was not thrown"));

// After
Assert.Fail("Expected exception was not thrown");

Files Changed

Area Count
System.Runtime tests 173
System.Threading.Tasks.Parallel tests 2
System.Reflection.MetadataLoadContext tests 2
GCLogParser 2

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove unnecessary string.Format calls without arguments Remove no-op string.Format calls with single message argument Jan 31, 2026
Copilot AI requested a review from stephentoub January 31, 2026 14:40
@am11
Copy link
Member

am11 commented Jan 31, 2026

There are also $"const string" interpolations, where we can omit $:

$ git grep -P '\$"(?![^"]*\{)[^"]*"' ':/*.cs' | wc -l
    2953

@jkotas
Copy link
Member

jkotas commented Jan 31, 2026

There are also $"const string" interpolations, where we can omit $:

It is common for people to use $ in logging code even when it is not strictly necessary so that they do not have think about adding/removing it when they add/remove inline formatting sequence. C# compiler is smart enough to optimized it out when it is not necessary. Console.WriteLine($"Hello"); produces exact same IL as Console.WriteLine("Hello");

@am11
Copy link
Member

am11 commented Jan 31, 2026

It is common for people to use $ in logging code even when it is not strictly necessary so that they do not have think about adding/removing it when they add/remove inline formatting sequence.

Btw, the 'structured logging' in ASP.NET doesn't require $ https://learn.microsoft.com/aspnet/core/fundamentals/logging/?view=aspnetcore-10.0#log-message-template. Adding $ as-needed should be ok especially when user would anyway need to modify the string contents.

C# compiler is smart enough to optimized it out when it is not necessary. Console.WriteLine($"Hello"); produces exact same IL as Console.WriteLine("Hello");

Cool. They can apply the same optimization to string.Format("const").

@jkotas
Copy link
Member

jkotas commented Jan 31, 2026

They can apply the same optimization to string.Format("const").

$ is C# language feature, so it is very natural for Roslyn to optimize it. It is not the case for string.Format.

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 removes unnecessary string.Format() wrapper calls where a single string literal is passed without any format arguments. This is a code cleanup that improves readability and reduces unnecessary method call overhead.

Changes:

  • Removed 239 no-op string.Format("message") calls across 18 files
  • All changes are in test files and the GCLogParser tool (no production code affected)
  • Every removed call was verified to have no format placeholders in the string

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Threading.Tasks.Parallel/tests/ParallelStateTest.cs Removed 2 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/TaskScheduler/TaskSchedulerTests.cs Removed 8 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskStatusTest.cs Removed 3 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskRunSyncTests.cs Removed 7 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskRtTests_Core.cs Removed numerous no-op string.Format calls from Assert.Fail statements in task completion tests
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskRtTests.cs Removed 4 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskFromAsyncTest.cs Removed 6 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskCreateTest.cs Removed 3 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskContinueWith_ContFuncAndActionWithArgsTests.cs Removed 7 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskContinueWithTests.cs Removed 10 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskContinueWithAllAnyTests.cs Removed 6 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskContinueWhenAnyTests.cs Removed 5 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskContinueWhenAllTests.cs Removed 3 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/Task/TaskCancelWaitTest.cs Removed 7 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/System.Runtime.CompilerServices/YieldAwaitableTests.cs Removed 6 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Runtime/tests/System.Threading.Tasks.Tests/CancellationTokenTests.cs Removed 4 no-op string.Format calls from Assert.Fail statements
src/libraries/System.Reflection.MetadataLoadContext/tests/src/Tests/TypeInfoFromProjectN/TypeInfo_PropertyTests.cs Removed 2 no-op string.Format calls from Assert.True/False statements
src/coreclr/tools/GCLogParser/parse-hb-log.cs Removed 2 no-op string.Format calls used for string concatenation

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.

4 participants