Visual Studio 2015:插值字符串表达式中的"Cast is redundant"警告无效

sst*_*tan 5 c# casting string-interpolation c#-6.0 visual-studio-2015

考虑一下这个在Visual Studio 2015中编译得很好的简单程序:

public class Program
{
    enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    static void Main(string[] args)
    {
        // Old style
        Console.WriteLine(string.Format("The direction is {0}", Direction.Right));
        Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));

        // New style
        Console.WriteLine($"The direction is {Direction.Right}");
        Console.WriteLine($"The direction is {(int)Direction.Right}");
    }
}
Run Code Online (Sandbox Code Playgroud)

...按预期输出:

The direction is Right
The direction is 3
The direction is Right
The direction is 3
Run Code Online (Sandbox Code Playgroud)

但是,Visual Studio 2015特别建议在此行中建议"快速操作":

// "Cast is redundant" warning
Console.WriteLine($"The direction is {(int)Direction.Right}");
Run Code Online (Sandbox Code Playgroud)

它坚持认为(int) "演员是多余的",并建议作为"删除不必要的演员"的潜在修复,这当然是错误的,因为它会改变结果.

有趣的是,它没有给我任何相应声明的警告:

// No warnings.
Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));
Run Code Online (Sandbox Code Playgroud)

当在插值字符串中使用表达式时,有人能为这种误报提供合理的解释吗?

Jer*_*vel 7

这是一个已知的错误.

已经提出了一个临时修复的平均时间:

对于现在在VS2015中遇到此错误的人,解决方法是在受影响项目的属性页的构建选项卡中禁止警告IDE0004.

这已经修复并于2015年9月9日在PR 5029合并为主人.