![]() |
![]() |
3402 | ![]() |
||||
![]() | |||||||
| Bracing and Indentation | REFERENCE - ISO:C90-6.6.4.1 The if statement - Semantics | ||||||
Message 3402 is generated when 2 nested if statements are followed by a single else clause and the body of the first if statement is not a compound statement. The absence of braces can be very misleading in such a situation because it is not obvious whether the else clause is related to the first or the second if statement. According to the ISO standard: "An else is associated with the lexically immediately preceding "else-less" if that is in the same block (but not in an enclosed block)." Braces should always be used following an if or an else so as to avoid any possible confusion. For example:
/*PRQA S 2017,2201,3120,3227,3408 ++*/
extern int foo(int i, int j)
{
int r = 0;
if (i > 0) /* Message 3402 */
if (j > 0) /* Message 2212 */
{
r = 1;
}
else
{
r = 2;
}
if (i > 0) /* Message 3402 */
if (j > 0) /* Message 2212 */
{
r = 1;
}
else
{
r = 2;
}
return r;
}
MISRA C:2012 Rules applicable to message 3402:
| Rule-15.6 (Required) | The body of an iteration-statement or a selection-statement shall be a compound-statement |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |