下面的程序編譯在Visual Studio 2015更新2細,而是一個「角色是多餘的」被生成警告:「角色是多餘的」警告當返回否定布爾
class Program
{
static void Main(string[] args)
{
Program p = new Program();
bool negated = p.NegateBool(true);
}
public bool NegateBool(object value)
{
// "Cast is redundant" warning.
return !(bool)value;
// No warning.
//return (bool)value;
// No warning.
//var negated = !(bool)value;
//return negated;
}
}
當接受了「快速修復」建議刪除演員,該程序不再編譯。
只有在直接在返回語句中完成強制轉換並使用了否定運算符時纔會發生這種情況。
如果結果放入一個變量然後返回,則不會生成警告。
有誰知道爲什麼會發生這種情況?
你或許應該提交與羅斯林團隊的bug報告。他們通常在Github上,而且非常有幫助。 – Joey
是的,如果你還沒有,請在https://github.com/dotnet/roslyn上提交一個錯誤。 –
我已經提交了一個錯誤報告。 –