roslynator
roslynator copied to clipboard
RCS1198: false positive with interpolated strings in .NET 6
Product and Version Used: Microsoft Visual Studio Community 2019 Version 16.10.4 Roslynator.Formatting.Analyzers 1.2.1 via Nuget RCS1198 IDE0071
Steps to Reproduce:
enum Foo
{
A = 0
}
var test1 = $"Test foo = {Foo.A}."; // Get diagnostic for RCS1198.
var test2 = $"Test foo = {Foo.A.ToString()}."; // Get diagnostic for IDE0071.
Actual Behavior: Get diagnostic for RCS1198 in interpolated strings.
Expected Behavior:
No diagnostic for RCS1198 in interpolated strings, because ToString() would be implicitly invoked by the compiler.
The purpose of this analyzer is to prevent boxing and the boxing will occur if the ToString call is missing.
The compiler will convert the code to:
string.Format("Test foo = {0}.", Foo.A);
https://sharplab.io/#v2:D4AQTAjAsAULIGYAE4kGFYG9ZNy5IALEgLIAUAlDntjHvUgG4CGATkgC4CmAzhxEgC8SACQAiACq8OSAGYB7eUKSYAYooB0AQQC+GsQG5quHbFNwYXAHYBXALZJ18rLACQW5QAYzsWEA
@JosefPihrt, it looks like Microsoft's documentation is misleading.

Thanks for the explanation. I will open the corresponding issue in .NET repository.
@JosefPihrt you will need to update this analyzer for .NET 6: with the interpolated string changes and DefaultInterpolatedStringHandler, no boxing is incurred here.
@333fred Thanks for the info.