Returning inside the switch block gives an "unreachable code" warning

I am writing UnityScript code and at some point in my program I want to end a function from a block switch. This is my version of my code:

function Move(target: int) {
    var targetTransform : Transform;
    switch (target) {
    case 0:
        // do something including assigning targetTransform
        break;

    case 1:
        // do something including actually moving my object
        return; // since I moved already, I want the function to terminate here

    default:
        // do something including assigning targetTransform
    }
    object.Move(targetTransform); // object is locally available
}

Now for some reason the compiler gives me

Assets/Scripts/GameMaster.js(490,9): BCW0015: WARNING: Unreachable code detected.

A line is a line containing switch.

From my previous research, I found many similar problems, but they all had actually unattainable code, for example, breakfor operators after returnor returnat the end, for example , although each case returned at some point. This is not the case here, I just want one of mine caseto return completely from the function, and the rest breakfrom switchand continue from there.

? , ?
, boolean switch... case .

+4
1

, , 2010. return break, .

  • .
  • , return boolean + break boolean switch

, suppress . #, UnityScript, , . , : #: -)

+4

Source: https://habr.com/ru/post/1541674/


All Articles