Search code examples
while-loopuncrustify

Newline between bodieless while loops and semicolon


While using uncrustify, I encountered a problem with while loops. If the while loop has no body, a trailing semicolon will get pushed to the next line. On the GitHub and on this website I found no mention of this whatsoever. Is the problem related to forcing one liners in different lines?

The code I tried it with is: (This is also what it should look like in the end)

int main()
{
    int i = 20000;
    while(i--);
    return 0;
}

But what uncrustify returns is:

int main()
{
    int i = 20000;
    while (i--)
        ;
    return 0;
}

Config file: https://pastebin.com/3FUqHmp8


Solution

  • With an empty config file this does not happen so this behavior is caused by a option that you added in the config file that you are using (most likely one of the nl_* options).

    Post a link to your file.


    Both of this options are causing this behavior:

    # Add or remove newline between 'while' and '{'.
    nl_while_brace                  = force    # ignore/add/remove/force
    
    # Change a one-liner while statement into simple unbraced while
    # 'while (i<5) foo(i++);' => 'while (i<5)\n foo(i++);'.
    nl_split_while_one_liner        = true     # false/true
    

    Keep in mind that Uncrustify also considers missing braces (virtual braces) to be braces (nl_while_brace).

    Uncrustify has an option to disable handling of vbraces for spacing options but it seems that it is missing it for newline options. If you need that open up a feature or pull request on the github repository site: https://github.com/uncrustify/uncrustify.