Main Content

Unreachable code

Code not executed because of preceding control-flow statements

Description

This defect occurs when a section of code cannot be reached because of a previous break in control flow using one these statements:

  • break and return: Statements such as break and return, move the flow of the program to another section or function. Because of this flow escape, the statements following the control-flow code cannot execute, and therefore the statements are unreachable.

  • goto: A statement such as goto causes a jump in the program flow to another section of the code. Unless there is a jump back to the immediate statements following the goto, these statements cannot be executed.

  • while(1): Trivial infinite loops, such as while(1) release the flow of the program by exiting the program. This type of exit causes code after the infinite loop to be unreachable.

Risk

Unreachable code wastes development time, memory and execution cycles. Developers have to maintain code that is not being executed. Instructions that are not executed still have to be stored and cached.

Fix

The fix depends on the intended functionality of the unreachable code. If you want the code to be executed, check the placement of the code or the prior statement that diverts the control flow. For instance, if the unreachable code follows a return statement, you might have to switch their order or remove the return statement altogether.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

Examples

expand all

typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit;
suit nextcard(void);
void guess(suit s);

suit deal(void){
    suit card = nextcard();
    if( (card < SPADES) || (card > CLUBS) ) 
        card = UNKNOWN_SUIT;
        return card;

    if (card < HEARTS) {
        guess(card);
    }
    return card;
}

In this example, the first if block is missing braces. As a result, in all execution contexts of the function deal(), the first return statement changes the flow of code back to where the function was called. Because of this return statement, the second if-block and second return statement cannot execute.

If you correct the indentation and the braces, the error becomes clearer.

typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit;
suit nextcard(void);
void guess(suit s);

suit deal(void){
    suit card = nextcard();
    if( (card < SPADES) || (card > CLUBS) ){ 
        card = UNKNOWN_SUIT;
    }
    return card;

    if (card < HEARTS) {
        guess(card);
    }
    return card;
}

Correction — Remove Return

One possible correction is to remove the escape statement. In this example, remove the first return statement to reach the final if statement.

typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit;
suit nextcard(void);
void guess(suit s);

suit deal(void){
    suit card = nextcard();
    if( (card < SPADES) || (card > CLUBS) )
    {
        card = UNKNOWN_SUIT;
    }

    if(card < HEARTS)
    {
        guess(card);
    }
    return card;
}
Correction — Remove Unreachable Code

Another possible correction is to remove the unreachable code if you do not need it. Because the function does not reach the second if-statement, removing it simplifies the code and does not change the program behavior.

typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit;
suit nextcard(void);
void guess(suit s);

suit deal(void){
    suit card = nextcard();
    if( (card < SPADES) || (card > CLUBS) )
    {
        card = UNKNOWN_SUIT;
    }
    return card;
}
int add_apples(int apple) { 
    int count = 1;
    while(1) {
        if(apple < 99){
            apple++; 
            count++;
        }else{
            count--;
        }
    }
    return count;
}

In this example, the while(1) statement creates an infinite loop. The return count statement following this infinite loop is unreachable because the only way to exit this infinite loop is to exit the program.

Correction — Rewrite Loop Condition

One possible correction is to change the loop condition to make the while loop finite. In the example correction here, the loop uses the statement from the if condition: apple < 99.

int add_apples1(int apple) { 
    int count = 0;
    while(apple < 99) { 
        apple++; 
        count++;
    }
    if(count == 0)
        count = -1;
    return count;
}
Correction — Add a Break Statement

Another possible correction is to add a break from the infinite loop, so there is a possibility of reaching code after the infinite loop. In this example, a break is added to the else block making the return count statement reachable.

int add_apples(int apple) { 
    int count = 1;
    while(1) {
        if(apple < 99)
        {
            apple++; 
            count++;
        }else{
            count--;
            break;
        }
    }
    return count;
}
Correction — Remove Unreachable Code

Another possible correction is to remove the unreachable code. This correction cleans up the code and makes it easier to review and maintain. In this example, remove the return statement and change the function return type to void.

void add_apples(int apple) { 
    int count = 1;
    while(1) {
        if(apple < 99)
        {
            apple++; 
            count++;
        }else{
            count--;
        }
    }
}

Result Information

Group: Data flow
Language: C | C++
Default: On
Command-Line Syntax: UNREACHABLE
Impact: Medium

Version History

Introduced in R2013b