Main Content

MISRA C++:2023 Rule 0.0.1

A function shall not contain unreachable statements

Since R2024b

Description

Rule Definition

A function shall not contain unreachable statements.

Rationale

Statements that are unreachable from the entry point of a function often indicate an error in the program logic. Unless the program is using an undefined behavior, an unreachable block cannot be executed and does not have any effect on the program outputs.

Polyspace Implementation

The rule checker reports a violation:

  • When a section of code cannot be reached because of a previous break in control flow using one of these statements:

    • break and return

    • goto

    • Trivial infinite loops such as while(1)

  • When a catch statement is not reached because a previous catch statement handles the exception thrown.

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

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) {   // Noncompliant - code follows return
        guess(card);
    }
    return card;
}

suit deal_fixed (void){
    suit card = nextcard();
    if( (card < SPADES) || (card > CLUBS) )  {
        card = UNKNOWN_SUIT;
        return card;
    }
    if (card < HEARTS) {   // Compliant
        guess(card);
    }
    return card;
}

In this example, there is a defect in the function deal() because the first if block is missing braces. Because of the missing braces, the first return statement always changes the flow of code back to where the function was called. The following if-block and the second return statement cannot execute in any context.

This issue is fixed in the function deal_fixed(). This function correctly puts braces around the first if block and avoids statically unreachable code following the return statement in the block.

#include <new>

extern void print_str(const char* p);
extern void throw_exception();

void func() {
    try {
        throw_exception();
    }
    catch(std::exception& exc) {
        print_str(exc.what());
    }

    catch(std::bad_alloc& exc) {  //Noncompliant
        print_str(exc.what());
    }
}

void func_fixed() {
    try {
        throw_exception();
    }
    
    catch(std::bad_alloc& exc) {  // Compliant
        print_str(exc.what());
    }
    catch(std::exception& exc) {
        print_str(exc.what());
    }
}

In this example, the second catch statement in the function func() accepts a std::bad_alloc object. Because the std::bad_alloc class is derived from the std::exception class, the second catch statement is hidden by the previous catch statement that accepts a std::exception object.

The function func_fixed() fixes this issue by reordering the catch statements.

Check Information

Group: Language independent issues
Category: Required

Version History

Introduced in R2024b