Rule 15.5 and return statements as children of labels

Moderators: misra-c, david ward

rgamble
Posts: 14
Joined: Fri Jul 20, 2007 7:54 pm

Rule 15.5 and return statements as children of labels

Postby rgamble » Fri Mar 29, 2019 1:33 pm

Rule 15.5 says:

When a return statement is used, it should be the final statement in the compound statement that forms the body of the function.


Consider the following example:

Code: Select all

typedef unsigned uint32_t;
uint32_t ten(uint32_t var) {
    if (var == 10) {
       return 10;
    }
}


I think it is pretty clear that this violates Rule 15.5 as the return statement is a child statement of the body of the if, even though no other statements appear between the return statement and the end of the function. Is this correct?

What then about the following:

Code: Select all

typedef unsigned uint32_t;
uint32_t ten() {
my_label:
    return 10;
}


The return statement is technically a child of the label statement so the "final statement in the compound statement that forms the body of the function" is a label statement according to the grammar of C. Is this intended to be a violation of Rule 15.5?

Return to “8.15 Control flow”

Who is online

Users browsing this forum: No registered users and 0 guests