WARNING! Unreachable code detected!

I’m getting this error with a particular function:

function Firing() : boolean {
	if( !pilot ){
		return false;
	}
	switch( mode ) {
		case WeaponMode.off:
			return false;
			break;
		case WeaponMode.primary:
			return pilot.fireMain;
			break;
		case WeaponMode.secondary:
			return pilot.fireSecondary;
			break;
	}
}

Note that the function is being called and works fine … it just generates this warning on line 1.

I’ve tried restarting Unity.

I’ve tried removing the (currently redundant) breaks (but I think that’s bad coding practice).

Any ideas?

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.

–Eric

Yeah, switch does this–need to set up a temp return variable and set it in switch, return after to avoid the error.

There’s also annoying unused private method errors if you invoke them via StartCoroutine/Invoke/etc…

1 Like

Ah OK. I searched but mustn’t have seen the thread or guessed the search terms. I hate compiler warnings, so I’ll code around it.

I know this is an old post, but I thought I’d append it anyway since I recently hit a similar bump.

I believe the cause of the warning is the “break” after a return command, which itself is a break. The ‘break’ code won’t ever be used.

you’ll see the same response if you throw “break;break;” in somewhere.

easy to miss when it becomes so natural to break every case of a switch.

2 Likes

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.

EDIT: Oh wow, that was an old post.

1 Like

hehe ye old.

But anyways since its alive again.

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. =)

//perlohmann

Agreed, perl.

It was probably just force of habit.
Though it is a good safety net to use a return after the switch, where applicable, to prevent void returns.

Using the default case also works. Less code overall that way.

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 :sweat_smile: .

Surely shorter methods would help a lot to avoid this sort of issues :smile:.

Bumping old thread.

You get this warning even if you JUST return in a switch and don’t break.

Bump! This is still around
e.g (silly test function!):

function test(t : int) : int
{
	switch(t)
	{
		case 1: return 1;
		case 2: return 2;
		case 3: return 3;
	}
	return 0;
}

gives the error

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.

Jump Statements in C#

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.

Old thread, but 100% correct answer Beezir.

Imperative is as imperative does.

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.