[previous] 2214 [next] Body of control statement is on the same line and is not enclosed within braces.
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.

The examples below demonstrate how unhelpful layout can create the dangerous illusion that the "++y" statement is within the control of the if statement when it is not.


/*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;
    }

}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents