未验证 提交 8290d189 编写于 作者: N Neal Gafter 提交者: GitHub

The default label is considered reachable in a traditional switch statement (#34021)

Fixes #33783
上级 8fc6a345
......@@ -30,7 +30,8 @@ private void VisitSwitchBlock(BoundSwitchStatement node)
{
foreach (var label in section.SwitchLabels)
{
if (reachableLabels.Contains(label.Label) || label.HasErrors)
if (reachableLabels.Contains(label.Label) || label.HasErrors ||
label == node.DefaultLabel && node.Expression.ConstantValue == null && IsTraditionalSwitch(node))
{
SetState(initialState.Clone());
}
......
......@@ -2918,6 +2918,35 @@ public static void Main()
);
}
[Fact]
[WorkItem(33783, "https://github.com/dotnet/roslyn/issues/33783")]
public void UnreachableDefaultInBoolSwitch()
{
var text = @"
public class TestClass
{
public static void Main()
{
bool b = false;
switch (b)
{
case true:
break;
case false:
break;
default:
break; //1
}
}
}";
CreateCompilation(text, parseOptions: TestOptions.Regular6).VerifyDiagnostics();
CreateCompilation(text, parseOptions: TestOptions.Regular7_3).VerifyDiagnostics();
CreateCompilation(text).VerifyDiagnostics(
// (14,17): warning CS0162: Unreachable code detected
// break; //1
Diagnostic(ErrorCode.WRN_UnreachableCode, "break").WithLocation(14, 17));
}
#endregion
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册