Search Unity

WARNING! Unreachable code detected!

Discussion in 'Scripting' started by podperson, Aug 29, 2008.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I'm getting this error with a particular function:

    Code (csharp):
    1. function Firing() : boolean {
    2.     if( !pilot ){
    3.         return false;
    4.     }
    5.     switch( mode ) {
    6.         case WeaponMode.off:
    7.             return false;
    8.             break;
    9.         case WeaponMode.primary:
    10.             return pilot.fireMain;
    11.             break;
    12.         case WeaponMode.secondary:
    13.             return pilot.fireSecondary;
    14.             break;
    15.     }
    16. }
    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?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    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...
     
    Scissors likes this.
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    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.
     
  5. Alec-Slayden

    Alec-Slayden

    Joined:
    Dec 16, 2008
    Posts:
    101
    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.
     
    latinslackin and Shippety like this.
  6. beezir

    beezir

    Joined:
    Apr 3, 2009
    Posts:
    133
    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.
     
    soheil47g likes this.
  7. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    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
     
  8. Alec-Slayden

    Alec-Slayden

    Joined:
    Dec 16, 2008
    Posts:
    101
    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.
     
  9. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    337
    Using the default case also works. Less code overall that way.
     
  10. ooppari

    ooppari

    Joined:
    Jan 29, 2009
    Posts:
    68
    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 :oops: .

    Surely shorter methods would help a lot to avoid this sort of issues :D.
     
  11. ofaaron

    ofaaron

    Joined:
    May 23, 2010
    Posts:
    52
    Bumping old thread.

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

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    Bump! This is still around
    e.g (silly test function!):
    Code (csharp):
    1.  
    2. function test(t : int) : int
    3. {
    4.     switch(t)
    5.     {
    6.         case 1: return 1;
    7.         case 2: return 2;
    8.         case 3: return 3;
    9.     }
    10.     return 0;
    11. }
    12.  
    gives the error
     
    Last edited: Oct 21, 2010
  13. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Code (csharp):
    1. function test(t : int) : int
    2. {
    3.     var value : int;
    4.     switch(t)
    5.     {
    6.         case 1: value = 1; break;
    7.         case 2: value = 2; break;
    8.         case 3: value = 3; break;
    9.         default: value = 0; break;
    10.     }
    11.     return value;
    12. }
    13.  
     
  14. pR0Ps

    pR0Ps

    Joined:
    Jan 29, 2011
    Posts:
    1
    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?
     
    Last edited: Feb 26, 2011
  15. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    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).
     
  16. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    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#
     
  17. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,276
    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.
     
  18. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    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.
     
  19. dmitche3

    dmitche3

    Joined:
    Apr 3, 2014
    Posts:
    23
    Old thread, but 100% correct answer Beezir.
     
  20. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    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.
     
    Last edited: Apr 4, 2014
  21. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    Wow, this is a really old thread, but it's still an issue I'm struggling with in 5.3.

    The reason it's still here is because it's not a bug that needs fixing -- it's not a bug at all, as a matter of fact. As previously stated, Unity throws this warning at you to inform you that any code that follows the return would be unreachable (if in the same case). It's not a bug at all.

    However, in my case, I have a huge switch statement (consisting of 39 cases) attached to several objects. For each case in each switch, it logs this warning message after every change. This actually causes lag upon entering play mode and it takes about a minute to enter play mode.

    I've been able to tolerate it to this point, but I'm going to need more switch statements, so the problem will only keep getting worse.

    I will attempt the work around and I'll report if it works in 5.3 or not.

    EDIT: Workaround works just fine for me in 5.3. Just a thing to note that doesn't apply to the original question in this thread: Use break instead of return if you don't need to return a variable. Break is much more efficient. :)
     
    Last edited: Jan 8, 2016
  22. Bothorth

    Bothorth

    Joined:
    Jan 17, 2014
    Posts:
    1
    Necro thread!

    Just for the record, converting the "return;" command into a conditional "if (true) return;" doesn't work. It still complains that the "break;" is unreachable. It knows! It knows!

    I've commented out all my breaks anyway. There are other warning messages I need to see.
     
  23. BloodFox__88

    BloodFox__88

    Joined:
    Dec 27, 2012
    Posts:
    2
    Worked For me Thanks!
     
  24. MiguelSC81

    MiguelSC81

    Joined:
    Apr 18, 2017
    Posts:
    1
    pivot_RyomaShibata likes this.