Code is heuristically unreachable for break statements

In build 1535, if you have something like this:

switch(whatever)
{
  case 1:
    return;
    break;
  case 2:
    Assert.Fail();
    break;
}


R# shows that "The code is heuristically unreachable" warning for both break statements.

While true, in this case, the break statements are syntactically mandatory, so the warning is incorrect.

3 comments

As a note, the first case's break statement is not syntatically mandatory (return negates the need for break). As such, your bug report only applies to the second case.

0

Obviously, you're right. I meant to show two opposite examples to highlight the problem, but misspoke.

The point is that R# knows that Assert.Fail() (and other annotated method calls) stop the stack execution, but doesn't consider cases in which subsequent calls are mandatory. There are a few examples for these kinds of cases:

int selector = 1;
switch(selector)
{
  case 2:
    Assert.Fail();
    break;
  case 3:
    Assert.Fail();
    Foo();
    break;
  default:
    Foo();
    Assert.Fail();
    break;
}


If selector == 1, "case 2" and "case 3" should be recognized as completely unreachable.

If selector == 3, "case 3" is reachable, but only "Foo();" is unreachable. The "break;" under case 3 should not be marked as unreachable.

0

Based on your second example, the minimum behaviour that I would be expecting from R# would be to warn me that the Foo() call in case 3 is unreachable and, as the compiler would, to warn me about any missing break/return statements. If R# also warned based on the specific static value of selector, I'd see that as a bonus but certainly wouldn't mind.

0

Please sign in to leave a comment.