Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB Unreachable code error/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.

like image 827
Binary Worrier Avatar asked Mar 31 '09 13:03

Binary Worrier


People also ask

Why does it say my code is unreachable?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

How do I stop unreachable code?

Have an infinite loop before them: Suppose inside “if” statement if you write statements after break statement, then the statements which are written below “break” keyword will never execute because if the condition is false, then the loop will never execute.


2 Answers

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.

like image 72
JaredPar Avatar answered Sep 21 '22 17:09

JaredPar


They mention this in the following post:

https://stackoverflow.com/questions/210187/usage-statistics-c-versus-vb-net

See the last post.

I guess you could use FXCop to check your code instead or get a copy of Resharper from:

http://www.jetbrains.com/resharper/

like image 31
Justin Bannister Avatar answered Sep 19 '22 17:09

Justin Bannister