Main Content

Dead code (DEAD_CODE)

R2026b

Code does not execute

Description

This defect occurs when a block of code cannot be reached because of a condition that is always true or false. Starting in R2026b, this defect also detects controlling expressions that are compile-time constants evaluating to zero or false. These expressions include if(0), while(0), if(false), macros that evaluate to false, and template constant-false conditions.

This defect excludes:

  • Unreachable code, which checks for code after a control escape such as goto, break, or return.

  • Useless if, which checks for if statements that are always true.

Risk

Dead 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.

Dead code often represents legacy code that is no longer used. Cleaning up dead code periodically reduces future maintenance.

Fix

The fix depends on the root cause of the defect. For instance, the root cause can be an error condition that is checked twice on the same execution path, making the second check redundant and the corresponding block dead code.

Often the result details (or source code tooltips in Polyspace® as You Code™) show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show this event history, you can search for previous references of variables relevant to the defect using right-click options in the source code and find related events. See also Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).

See examples of fixes below.

If you see dead code from use of functions such as isinf and isnan, enable an analysis mode that takes into account non-finite values. See Consider non finite floats (-allow-non-finite-floats).

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

Examples

expand all

#include <stdio.h>

int Return_From_Table(int ch){

    int table[5];

    /* Create a table */
    for(int i=0;i<=4;i++){
        table[i]=i^2+i+1;
    }

    if(table[ch]>100){ /* Defect: Condition always false */
         return 0;  
    }
    return table[ch];
}

The maximum value in the array table is 4^2+4+1=21, so the test expression table[ch]>100 always evaluates to false. The return 0 in the if statement is not executed.

Correction — Remove Dead Code

One possible correction is to remove the if condition from the code.

#include <stdio.h>

int Return_From_Table(int ch){

    int table[5];

    /* Create a table */
    for(int i=0;i<=4;i++){
        table[i]=i^2+i+1;
    }

    return table[ch];
}
typedef enum _suit {UNKNOWN_SUIT, SPADES, HEARTS, DIAMONDS, CLUBS} suit;
suit nextcard(void);
void do_something(suit s);

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

    if (card > 7) {
        do_something(card);
    }
}

The type suit is enumerated with five options. However, the conditional expression card > 7 always evaluates to false because card can be at most 5. The content in the if statement is not executed.

Correction — Change Condition

One possible correction is to change the if-condition in the code. In this correction, the 7 is changed to HEART to relate directly to the type of card.

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

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

    if (card > HEARTS) {
        do_something(card);
    }
}

In this example, Polyspace Bug Finder™ detects dead code from a controlling expression that always evaluates to zero at compile time.

#include <stdlib.h>

#define DEBUG_MODE 0

void process_data(int *data, int size) {
    for (int i = 0; i < size; i++) {
        data[i] = data[i] * 2;
    }

    if (DEBUG_MODE) { // defect 
        /* Log processing details */
        for (int i = 0; i < size; i++) {
            /* print debug info */
        }
    }
}

The controlling expression DEBUG_MODE is a macro that evaluates to 0 at compile time. The body of the if statement never executes and is dead code. Polyspace Bug Finder reports this defect because the controlling expression is a compile-time constant that always evaluates to zero.

Correction — Remove or Guard Dead Code with Preprocessor Directive

One possible correction is to use a preprocessor directive instead of a runtime check. Alternatively, remove the dead code entirely.


#include <stdlib.h>

#define DEBUG_MODE 0

void process_data(int *data, int size) {
    for (int i = 0; i < size; i++) {
        data[i] = data[i] * 2;
    }

#if DEBUG_MODE
    /* Log processing details */
    for (int i = 0; i < size; i++) {
        /* print debug info */
    }
#endif
}

Result Information

Group: Data flow
Language: C | C++
Default: On
Command-Line Syntax: DEAD_CODE
Impact: Low
PQL Name: std.defects.DEAD_CODE

Version History

Introduced in R2013b

expand all