主要内容

MISRA C:2025 Rule 2.1

R2026b

A project shall not contain unreachable code

Since R2026b

Description

A project shall not contain unreachable code 1 .

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 reports a violation when:

  • Code follows a return, break, continue, or goto statement in the same block

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

  • The controlling expression of a selection or iteration statement is a compile-time constant that evaluates to zero

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 if statement has a controlling expression that is a compile-time constant evaluating to zero.


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

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

    return result;
}

Polyspace reports a violation because the body of the if statement is unreachable. The expression 0 always evaluates to false.

Correction — Remove Dead Code

Remove the unreachable block or replace the condition with a runtime expression.


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

    return result;          // Compliant - no unreachable code
}

Check Information

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

Version History

Introduced in R2026b

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.