roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

RCS1198: false positive with interpolated strings in .NET 6

Open anreton opened this issue 4 years ago • 4 comments
trafficstars

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.

anreton avatar Jul 28 '21 09:07 anreton

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 avatar Jul 29 '21 17:07 josefpihrt

@JosefPihrt, it looks like Microsoft's documentation is misleading.

image

Thanks for the explanation. I will open the corresponding issue in .NET repository.

anreton avatar Jul 29 '21 18:07 anreton

@JosefPihrt you will need to update this analyzer for .NET 6: with the interpolated string changes and DefaultInterpolatedStringHandler, no boxing is incurred here.

333fred avatar Jul 29 '21 22:07 333fred

@333fred Thanks for the info.

josefpihrt avatar Jul 31 '21 10:07 josefpihrt