![]() |
![]() |
2212 | ![]() |
||||
![]() | |||||||
| Bracing and Indentation | |||||||
The 'body' of this if, for, while or do while statement is not a compound statement, i.e. it is not enclosed in braces { }. Although, it is perfectly legal to write code in this way, there are potential dangers. In the absence of braces, the 'body' of the control statement will always be just a single statement; but it is easy to be misled by the way in which subsequent code is arranged. Two messages address this problem.
/*PRQA S 2017,2201,2205,3227,3408 ++*/
extern void foo(int n)
{
int x = 0;
int y = 0;
if (n > 0)
++x; /* Message 2212 */
++y;
if (n > 0) ++x; ++y; /* Message 2214 */
if (n > 0)
{
++x; /* OK */
++y;
}
}
MISRA-C:2004 Rules applicable to message 2212:
| Rule 14.8 (Required) | The statement forming the body of a switch, while, do ... while or for statement shall be a compound statement. |
| Rule 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. |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |