Already discussed…my guess is it’s the “unreachable code” routine being a tad overly enthusiastic. Since it’s just a warning rather than an error, I’d say ignore it for now.
As mentioned, this probably happens because you return before a break. I second the idea of storing the return value in a variable and returning after the entire switch block, but for a different reason. In this particular case, you probably catch all possible values… but if you happened to forget one, or added one to an enum you were switching on but forgot to add the case block, the function would end with no return value. Sticking it after the whole block guarantees that no matter what happens inside the function, you always return something.
Why even have the break after the return? it has no function as the compiler correctly states. it will never be called.
It is perfectly legal to have a return instead of a break in a switch they dont both have to be there. =)
I want also comment this ages old post ;). It’s bad coding habit to return in middle of method. It’s really annoying try to find error from method, which actually has returned in middle of method and you try to find error bottom part of method( in case you don’t notice that method has already returned ), this has happened few times to me .
Surely shorter methods would help a lot to avoid this sort of issues .
function test(t : int) : int
{
var value : int;
switch(t)
{
case 1: value = 1; break;
case 2: value = 2; break;
case 3: value = 3; break;
default: value = 0; break;
}
return value;
}
While that works, it’s just a workaround and does not deal with the main issue. The issue is that it is perfectly valid to use a return statement inside a switch/case structure and Unity should not be throwing “unreachable code” warnings.
Bug report submitted.
Edit: Damn, should have checked the date before posting, sorry. On another note, this bug is still around after this long?
This isn’t a bug. Unity is giving a perfectly valid warning. The simple fact is, you can either use a return value inside your case (and no break) OR you can set a value in the case statement, break out, then return the value. Either are perfectly fine. Doing both is redundant and pointless, and Unity will tell you this (hence the warning).
In C#, and presumably in UnityScript as well any one (and only one) of the following must finish a block in the switch statement:
break
continue
return
goto
I am not sure about UnityScript but apparently, unlike in other languages, “C# does not support an explicit fall through from one case label to another. If you want, you can use goto a switch-case, or goto default.”
still an issue this one. I guess this one was not fixed in 3.4. Work around above is fine, but warning is incorrect if return used no break since the code after is perfectly reachable.
Just noting that this bug seems to still be in Unity 4.2. Strangely, it only throws the unreachable code message for me sometimes.
And just to clarify: In the original post in this thread, the message is appropriate due to the break; after return;. However as others have since pointed out, the message still happens (sometimes?) when there’s only a return, which is valid and shouldn’t create a warning, even if it’s not generally best practice.
In more FP-inspired languages like F# / Scala, where if, switch, and blocks evaluate to a value and returns from a function are implicit, I (almost) never have multiple return statements or even use breaks. In C / C#, well, it’s often a helluvalot easier and cleaner just to roll with how the language (doesn’t) work and live with multiple return paths.