C++ Formatting one-line brace-enclosed statements

Viewed 150

My team's convention is to always use braces with conditions and loops with a line break before the opening brace, but for one-line statements we don't include any line breaks between braces. For example:

if( condition )
{ doIt(); }

We use clang-format to auto-format our code, but as far as I can tell this isn't a convention that clang-format can handle. The result is that I have to be very careful not to auto-format entire source files or I'm stuck fixing every one-line if, else, for, etc. I've found myself undoing the same auto-formatted lines in the same source files repeatedly.

I've just started using CLion, and I can easily enough set up its default formatting to match what we do with clang-format, but I still can't figure out how to match our brace-enclosed one-liner convention.

Is there a way to tell either CLion or clang-format to do this? The obvious solutions would be to either avoid auto-formatting altogether or convince my team to adopt a convention that's easier to accommodate, but neither is an option (they're a very nit-picky bunch with very firm opinions about code style).

1 Answers
  • You cannot follow the

    if( condition )
    { doIt(); }

    convenion in CLion native formatter, sorry.

  • CLion supports clang-format from the box.

Related