Search code examples
c++coding-styleclangclang-format

How to prevent clang-format from adding a single semicolon to a new line?


I have this line of code in C++

while (fread(pixel_array++, sizeof(byte), 3, fp));

but when I use clang-format, it splits the semicolon and add it into a new line

while (fread(pixel_array++, sizeof(byte), 3, fp))
    ;

I don't like this kind of style, and I just prefer to keep the original one.

How should I modify my clang-format configuration? Thanks.


Solution

  • clang-format 5.0 currently doesn't recognize that type of loop. Unfortunately as of clang-format version 5, you won't get a setting that does what you need.

    Looking up the Clang Format Style Options, the closest that I've found is AllowShortLoopsOnASingleLine: true, but that setting doesn't recognize the loop condition as being the body of the loop.

    As long as clang-format doesn't recognize those kinds of loops, what I'd do is to mark it your code with // clang-format off and then // clang-format on around your block of code.