5

Assuming we have the following (very basic code)

public int Foo()
{
    while (true)
    {

    }
    // No "return 0" etc. needed here.
}

the compiler can understand that this method will never return, and therefore displays a warning and also it does not require the method to have a return statement.


If we have the case

public void WontExit()
{
    while (true)
    {

    }
}

public int Foo()
{
    this.WontExit();
    return default(int); // This is needed here.
}

a return statement is needed, because the compiler can seemingly not foresee that it will never be reached.


  • Why does the compiler allow for omitting the return statement in the first case? Why doesn't it also require a return statement? (What are the internals here?)
  • Is there any way to indicate the compiler (or reachability analysis) that in the second case, the return code path will also never be reached?
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 1
    Why would `this.WontExit()` not exit in typical operation? Is this absolutely guaranteed? –  Feb 26 '18 at 16:16
  • 3
    As an aside, the given code *can* exit; ie via exception – BradleyDotNET Feb 26 '18 at 16:17
  • 1
    In the actual code it is absolutely guaranteed, because it will `throw` on all code paths. – Thomas Flinkow Feb 26 '18 at 16:17
  • 1
    I agree: it's possible for there to be a `return` statement inside that `while` loop which could exit the function, so it would be difficult for the compiler to guarantee that it would always run infinitely. – JJ Brown Feb 26 '18 at 16:18
  • 2
    @JackBrown you're right, that's why I wondered if there maybe was something like `[[noreturn]]` in C++, which can be used for exactly those cases when the programmer knows that the code will never return. – Thomas Flinkow Feb 26 '18 at 16:19

1 Answers1

9

Why does the compiler allow for omitting the return statement in the first case?

Because the final } is unreachable. That's the condition that the compiler prevents (and that's in the specification): ever being able to reach the end of a non-void method.

Is there any way to indicate the compiler (or reachability analysis) that in the second case, the return statement will also never be reached?

No, unfortunately. There are various times that would be useful (where it would be defined as "this method can never return normally, i.e. without throwing an exception"), but there's no C# feature for it. I believe Eric Lippert blogged about this at some point... will try to find the article.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You're probably thinking of [this one](https://ericlippert.com/2011/02/21/never-say-never-part-one/), which is linked in the comments of the dupe question. – Jeroen Mostert Feb 26 '18 at 16:26
  • 1
    @JeroenMostert: The end of it, yes - but I thought there was another one with more detail. Oh well. I'll leave this answer here despite it being a duplicate; in particular the first part is slightly different. – Jon Skeet Feb 26 '18 at 16:31
  • You might be thinking of a similar article I wrote for the Coverity blog which has since been taken down. – Eric Lippert Feb 27 '18 at 15:15
  • 1
    @EricLippert: Let's assume that's the one I was thinking of, as it's plausible and avoids me having to research any further :) – Jon Skeet Feb 27 '18 at 15:16