MISRA C++:2008 Rule 6-3-1
The statement forming the body of a switch, while, do while or for statement shall be a compound statement
Description
Rule Definition
The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement.
Rationale
A compound statement is included in braces.
If a 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;
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, the second line of the message states that the for
loop
is violating the rule.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2013b