question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IDE0071 may change type of objects passed to FormattableString

See original GitHub issue

Version Used: 3.8.0-4.20464.1+56f747b362e15a0763bad5ce4702a9b7c8949d7e

Steps to Reproduce:

using System;

class Program
{
    static void Main(string[] args) => M($"{args.Length.ToString()}");

    static void M(FormattableString fs)
    {
        foreach (object o in fs.GetArguments())
            Console.WriteLine(o?.GetType());
    }
}

Expected Behavior: IDE0071 does not fire (or a distinct diagnostic ID should be used for targeting FormattableString instead of string, or something like that)

Actual Behavior: IDE0071 fires, and applying the fix (removing the .ToString()) ends up changing the program’s behavior, outputting “System.Int32” instead of “System.String”. This is relevant when a FormattableString is being used by some utility that processes the objects in some way, based on type, e.g. in https://github.com/dotnet/runtime/blob/b39c698c628079b464650cda2540653533a84342/src/libraries/Common/src/System/Net/Logging/NetEventSource.Common.cs#L388-L392 where FormattableString is being used by logging and exerts customized formatting behavior based on argument type… in which case using ToString() in the interpolation is a way for the caller to opt-out of that policy.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
sharwellcommented, Sep 24, 2020

Given that FormattableString is an edge case usage, I would be fine with simply excluding instances of that type from analysis altogether. We could create a separate diagnostic for it, but it’s just not common enough to matter in practice.

1reaction
CyrusNajmabadicommented, Sep 22, 2020

I’m suggesting a separate ID based on the target, whether it’s String or FormattableString. They represent different scenarios. There is a clear split between them.

Fair, we can look into that as a particular fix for exactly this issue. However, as per above i’m wary about this as it would no mean extra user options for people to have to know about, extra UI to enable and distinguish these, etc. I’m not sure this case warrants it, but we can take to the team to decide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IDE0071: Simplify interpolation - .NET
It recommends removal of certain explicit method calls, such as ToString() , when the same method would be implicitly invoked by the ...
Read more >
c# - What is the original type of interpolated string?
I would not call it an implicit conversion. You cannot convert string to FormattableString . FormattableString needs more information than ...
Read more >
FormattableString - somewhat abstract
Prior to C#6, string interpolation (or string formatting) was ... From this object, we can get the `Format` string that could be passed...
Read more >
FormattableString and string interpolation - Siderite's Blog
I will then change the type of the parameter of Test to be FormattableString. It still works and it displays the same thing....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found