MISRA Discussion Forums
Rule 15.5 and return statements as children of labels - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.15 Control flow (https://forum.misra.org.uk/forumdisplay.php?fid=170)
+---- Thread: Rule 15.5 and return statements as children of labels (/showthread.php?tid=1485)



Rule 15.5 and return statements as children of labels - rgamble - 29-03-2019

Rule 15.5 says:

Quote: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:
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:
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?


Re: Rule 15.5 and return statements as children of labels - misra-c - 13-06-2019

Both your examples are violations of rule 15.5.

The MISRA C Working Group will review the use of labeled return statements.


RE: Rule 15.5 and return statements as children of labels - misra-c - 05-05-2023

The compound statement that forms the body of the function refers to the outer { } braces of the function - not any inner compound statement.

Therefore, your first example violates R.15.5 - it also violates Mandatory R.17.4 as there is no return statement in the path when the condition is false.

In your second example, a labelled-statement is formed by an id followed by a statement - therefore, the use of a labelled return is not a violation or R.15.5