Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code is only considered unreachable if it doesn't start with a throw statement #34625

Closed
jnm2 opened this issue Mar 31, 2019 · 2 comments
Closed
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design

Comments

@jnm2
Copy link
Contributor

jnm2 commented Mar 31, 2019

Version Used: 16.0.0 RC.3

As expected:

void M()
{
    return;
    // CS0162 Unreachable code detected
    _ = 42;
    throw new NotImplementedException();
}

Broken:

void M()
{
    return;
    // No warning or transparency
    throw new NotImplementedException();
}
@YairHalberstadt
Copy link
Contributor

That might be useful if you want to throw new InvalidOperationException("This location is thought to be unreachable");

@gafter gafter added the Resolution-By Design The behavior reported in the issue matches the current design label Apr 1, 2019
@gafter
Copy link
Member

gafter commented Apr 1, 2019

The following types of statements are not reported as unreachable (by design)

  • Block statements (however, statements within them may be reported unreachable)
  • Throw statements
  • Empty statements ;

We do this because that's what the older compiler did and we don't want to "break" existing code. I think these are justified philosophically, but in any case we won't change it.

@gafter gafter closed this as completed Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

4 participants