MISRAC++2023-9.3.1_c
Synopsis
(Required) The body of an iteration-statement or a selection-statement shall be a compound-statement
Enabled by default
Yes
Severity/Certainty
Low/Low

Full description
If and else statements shall have a body. The construct else if is allowed though. This check is identical to MISRAC++2008-6-4-1, MISRAC2004-14.9, MISRAC2012-Rule-15.6_c.
Coding standards
- CERT EXP19-C
Use braces for the body of an if, for, or while statement
- CWE 483
Incorrect Block Delimitation
- MISRA C:2004 14.9
(Required) An if expression construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement or another if statement.
- MISRA C:2012 Rule-15.6
(Required) The body of an iteration-statement or a selection-statement shall be acompound-statement
- MISRA C++ 2008 6-4-1
(Required) An if ( condition ) construct shall be followed by a compound statement. The else keyword shall be followed by either a compound statement, or another if statement.
Code examples
The following code example fails the check and will give a warning:
int foo();
void example(void) {
if (foo() > 0);
if (foo() > 2);
else;
}
The following code example passes the check and will not give a warning about this issue:
int foo();
void example(void) {
if (foo() > 0) {
}
if (foo() > 2) {
} else {
}
if (foo() > 3) {
} else if (foo() > 4) {
}
}