Skip to content

Do not report MA0184 when the interpolated string is converted to IFormattable - #1468

Merged
meziantou merged 1 commit into
mainfrom
feature/ma0184-iformattable-conversion-31b89f
Sep 12, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/ma0184-iformattable-conversion-31b89f

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Problem

MA0184 reported interpolated strings without holes that are converted to System.IFormattable, and its code fix broke the build:

public static System.IFormattable Run() => $"text";
// Fixed code: error CS0029, a string cannot be converted to System.IFormattable
public static System.IFormattable Run() => "text";

Interpolated strings support a conversion to IFormattable (like FormattableString), but a string literal does not. The analyzer only excluded FormattableString.

Changes

  • The analyzer now looks at the conversion applied to the interpolated string itself, and does not report when it targets FormattableString or IFormattable, or when the string is used by a custom interpolated string handler.
  • This replaces the walk up the parent chain, which looked at every enclosing operation. Only the conversion of the interpolated string itself matters: the conversions of the enclosing operations still apply to the string literal. As a consequence, a string nested in an expression of type FormattableString, such as FormattableString value = Create($"text") where Create takes a string, is now reported, and the fix is valid there.
  • docs/Rules/MA0184.md lists IFormattable in the exceptions.

Tests

New tests with an IFormattable target: 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 to object and for the Create($"text") case.

  • DoNotUseInterpolatedStringWithoutParametersAnalyzerTests: 20/20 on Roslyn 4.8, 4.14, 5.0, 5.6, and 5.9 (the 5 IFormattable tests failed before the fix)
  • Full Roslyn 5.9 test suite: 4387/4387
  • dotnet run --project src/DocumentationGenerator: no further changes

…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.
@meziantou
meziantou merged commit 1eb9be7 into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0184-iformattable-conversion-31b89f branch September 12, 2026 01:53
This was referenced Sep 12, 2026
This was referenced Sep 26, 2026
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.

1 participant