Search code examples
vb.netcompiler-construction

Is it possible for the Vb.Net compiler to switch on an "Unreachable code" warning?


I've been mostly working with VB.Net for over a year and just noticed this

Am I going insane, or does VB.Net NOT have an "Unreachable code" warning?

The following compiles quite happily with nary a warning or error, even though there is a return between the two writeline calls.

Sub Main()
    Console.WriteLine("Hello World")
    Return
    Console.WriteLine("Unreachable code, will never run")
End Sub

Am I missing something? Is there some way to switch this on that I can't find.

If not, is there a good reason for its omission? (i.e. or am I right in thinking this is a woeful state of affairs)

Forgive the air of rant about this question, it's not a rant, I would like an answer.

Thanks


I've raised this on MS Connect, as bug# 428529

Update

I received the following from the VB Teams program manager

Thanks for taking the time to report this issue. The compiler has limited support for this scenario, and as you point out we don't have warnings for unreachable code. There are some scenarios that our flow analysis algorithm does handle, such as the following:

Sub Main()
    Dim x As Integer
    Return
    x = 4
End Sub

In this case you'll get a warning that x has never been assigned. For the case you mentioned however we'll have to look at implementing that for a future release.


Solution

  • My guess is that it's an oversight in the compiler. Flow control is a very difficult problem to get correct in any language, but especially in a language like VB which has so many different flow control mechanisms. For instance,

    • Exceptions
    • Goto
    • On Error (Resume, Goto, etc ...)
    • Exit calls

    If you feel strongly about this issue, please file a bug on Connect. We do take bugs filed via Connect very seriously and do our best to fix as many as possible.