dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

dotnet format - Simplify interpolation (IDE0071) - false positive with Enum and FormattableString #40347

Open JackMcBride98 opened 6 months ago

JackMcBride98 commented 6 months ago

Describe the bug

We have the below Enum

public enum EmployeeType
{
    Azure,
    Manual,
}

Then when using FormattableSqlString

await dataContext.Database.ExecuteSqlInterpolatedAsync(
    $"UPDATE Employees SET Discriminator = {EmployeeType.Azure.ToString()}, AzureId = {azureId} WHERE EmployeeId = {employee.EmployeeId};");

This sets the Discriminator to "Azure". Without the .ToString() call on the enum, it sets it to 0

Running dotnet format removes the .ToString() call on the EmployeeType enum, which changes the behaviour. I've fixed for now by using nameof(), but this defo seems an issue.

Is this the correct place for this issue?

To Reproduce

public enum Test
{
    A,
    B
}
FormattableString x = $"{Test.A.ToString()}"

Run dotnet format on a project with this code in.

TargetFramework - net8.0

dotnet-issue-labeler[bot] commented 6 months ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

KalleOlaviNiemitalo commented 6 months ago

This seems a duplicate of https://github.com/dotnet/roslyn/issues/47956.