Main Content

MISRA C++:2008 Rule 0-1-9

There shall be no dead code

Description

Rule Definition

There shall be no dead code.

Rationale

If an operation is reachable but removing the operation does not affect program behavior, the operation constitutes dead code. For instance, suppose that a variable is never read following a write operation. The write operation is redundant.

The presence of dead code can indicate an error in the program logic. Because a compiler can remove dead code, its presence can cause confusion for code reviewers.

Polyspace Implementation

Polyspace® reports violation of this rule on statements that have no effect on the code, such as writing a value to a variable that is not used subsequently.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#define ULIM 10000

int func(int arg) {
    int res;
    res = arg*arg + arg;
    if (res > ULIM)
        res = 0; //Noncompliant
    return arg;
}

In this example, the operations involving res are redundant because the function func returns its argument arg. All operations involving res can be removed without changing the effect of the function.

The checker flags the last write operation on res because the variable is never read after that point. The dead code can indicate an unintended coding error. For instance, you intended to return the value of res instead of arg.

Check Information

Group: Language Independent Issues
Category: Required

Version History

Introduced in R2016b