Do not offer the MA0044 code fix when removing ToString would drop argument side effects - #1462
Merged
Merged
Conversation
…gument side effects string.ToString(IFormatProvider) ignores its provider, but C# still evaluates the argument. The fixer replaced the whole invocation with its receiver, so a call such as "text".ToString(Provider()) lost the evaluation of Provider(). The fix is now only registered when every argument can be discarded without observable effect (constants, locals, parameters, this, static fields or fields of this, static CultureInfo properties). The member-access check is also moved to the registration, so the fix is no longer offered for value?.ToString(), where it returned the document unchanged.
This was referenced Sep 12, 2026
This was referenced Sep 17, 2026
Merged
Open
Open
Open
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.
Problem
MA0044 reports any
ToStringcall onSystem.String, includingToString(IFormatProvider). The provider is ignored by that overload, but C# still evaluates the argument. The fixer replaced the whole invocation with its receiver, so side effects of the argument were silently removed:Changes
RemoveUselessToStringFixernow only registers the fix when every argument can be discarded without observable effect: constants and literals, locals, parameters,this, static fields or fields ofthis, and staticCultureInfoproperties (sostr.ToString(CultureInfo.InvariantCulture)is still fixed). Implicit conversions are looked through unless they call a user-defined operator.value?.ToString(), where it previously returned the document unchanged.docs/Rules/MA0044.mddocuments when the fix is not offered.Tests
Added to
RemoveUselessToStringAnalyzerTests:?.ToString()(these three fail against the previous fixer)CultureInfo.InvariantCulture,null, and a parameterThe test class passes (8/8) on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9. The full suite was not run locally.
Known gaps (not addressed here)
s.ToString()throws aNullReferenceException,sdoes not).