Do not report MA0184 when the interpolated string is converted to IFormattable - #1468
Merged
Merged
Conversation
…rmattable An interpolated string can be converted to IFormattable even without interpolation holes, but a string literal cannot, so the code fix produced CS0029. The analyzer now checks the conversion applied to the interpolated string itself instead of walking up the parent chain, which also stops it from ignoring strings that are only nested in an expression of type FormattableString.
This was referenced Sep 12, 2026
This was referenced Sep 26, 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.
Problem
MA0184 reported interpolated strings without holes that are converted to
System.IFormattable, and its code fix broke the build:Interpolated strings support a conversion to
IFormattable(likeFormattableString), but a string literal does not. The analyzer only excludedFormattableString.Changes
FormattableStringorIFormattable, or when the string is used by a custom interpolated string handler.FormattableString, such asFormattableString value = Create($"text")whereCreatetakes astring, is now reported, and the fix is valid there.docs/Rules/MA0184.mdlistsIFormattablein the exceptions.Tests
New tests with an
IFormattabletarget: return expression, local and field assignments, method argument, explicit cast, and conditional branch. New tests checking that the diagnostic and fix still apply for a conversion toobjectand for theCreate($"text")case.DoNotUseInterpolatedStringWithoutParametersAnalyzerTests: 20/20 on Roslyn 4.8, 4.14, 5.0, 5.6, and 5.9 (the 5IFormattabletests failed before the fix)dotnet run --project src/DocumentationGenerator: no further changes