[go: up one dir, main page]

Skip to content

Braces not added when control statement contains another control statement

When --add-braces is used, if one control statement contains another control statement, braces are only added to the inner-most control statement.

Input:

void test(void)
{
    if (cond1)
        if (cond2)
            if (cond3)
                if (cond4)
                    printf("test");

    if (cond)
        while (cond)
            printf("test");

    while (cond)
        if (cond)
            printf("test");

    if (cond)
        for (cond = 0; cond < 5; cond++)
            printf("test");

    for (cond = 0; cond < 5; cond++)
        if (cond)
            printf("test");

    do
        if (cond)
            printf("test");
    while (cond);

    while (cond)
        for (cond = 0; cond < 5; cond++)
            if (cond)
                printf("test");
}

Command: astyle --style=allman --add-braces test.c

Output:

void test(void)
{
    if (cond1)
        if (cond2)
            if (cond3)
                if (cond4)
                {
                    printf("test");
                }

    if (cond)
        while (cond)
        {
            printf("test");
        }

    while (cond)
        if (cond)
        {
            printf("test");
        }

    if (cond)
        for (cond = 0; cond < 5; cond++)
        {
            printf("test");
        }

    for (cond = 0; cond < 5; cond++)
        if (cond)
        {
            printf("test");
        }

    do
        if (cond)
        {
            printf("test");
        }
    while (cond);

    while (cond)
        for (cond = 0; cond < 5; cond++)
            if (cond)
            {
                printf("test");
            }
}

I expect the braces to be added for every do, if, for and while statement here.