MISRA C:2023 Rule 15.6
The body of an iteration-statement or a selection-statement shall be a compound statement
Since R2024a
Description
Rule Definition
The body of an iteration-statement or a selection-statement shall be a compound- statement.
Rationale
If the block of code associated with an iteration or selection statement is not contained in braces, you can make mistakes about the association. For example:
You can wrongly associate a line of code with an iteration or selection statement because of its indentation.
You can accidentally place a semicolon following the iteration or selection statement. Because of the semicolon, the line following the statement is no longer associated with the statement even though you intended otherwise.
This checker enforces the practice of adding braces following a selection or iteration statement even for a single line in the body. Later, when more lines are added, the developer adding them does not need to note the absence of braces and include them.
Polyspace Implementation
The checker flags for
loops where the first token following a
for
statement is not a left brace, for
instance:
for (i=init_val; i > 0; i--) if (arr[i] < 0) arr[i] = 0;
if
, else if
,
else
, switch
, for
and
do..while
statements.The second line of the message on the Result Details pane
indicates which statement is violating the rule. For instance, in the preceding
example, there are two violations. The second line of the message points to the
for
loop for one violation and the if
condition for another.
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
Check Information
Group: Control Flow |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2024a