Contenido principal

MISRA C:2012 Rule 2.1

R2026b

A project shall not contain unreachable code

Description

A project shall not contain unreachable code1 .

Rationale

Unless a program exhibits any undefined behavior, unreachable code cannot execute. The unreachable code cannot affect the program output. The presence of unreachable code can indicate an error in the program logic. Unreachable code that the compiler does not remove wastes resources, for example:

  • It occupies space in the target machine memory.

  • Its presence can cause a compiler to select longer, slower jump instructions when transferring control around the unreachable code.

  • Within a loop, it can prevent the entire loop from residing in an instruction cache.

Polyspace Implementation

Polyspace® reports a violation of this rule when a statement in your code is not reachable.

The checker flags these patterns as unreachable code:

  • A statement follows a return, break, or unconditional goto statement

  • A static function is defined but never called in the same translation unit

  • Code is inside a constant-false branch such as if(0)

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

enum light { red, amber, red_amber, green };

enum light next_light ( enum light color )
{
    enum light res;

    switch ( color )
    {
    case red:
        res = red_amber;
        break;
    case red_amber:
        res = green;
        break;
    case green:
        res = amber;
        break;
    case amber:
        res = red;
        break;
    default:
    {
        error_handler ();
        break;
    }
    }

    res = color;
    return res;
    res = color;     /* Non-compliant */
}

In this example, the rule is violated because there is an unreachable operation following the return statement.

In this example, the function contains a branch with a controlling expression that always evaluates to zero.


int process_value(int input) {
    int result = input * 2;

    if (0) {                  // Noncompliant - controlling expression is always false
        result = input + 1;
    }

    return result;
}

Polyspace reports a violation of this rule because the code inside the if(0) branch is unreachable. The controlling expression is a compile-time constant that evaluates to zero, so the statements inside the branch can never execute.

Check Information

Group: Unused Code
Category: Required
AGC Category: Required
PQL Name: std.misra_c_2012.R2_1

Version History

Introduced in R2014b

expand all


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C:2025

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.