Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove line break after single-statement if with clang-format

I am trying to get clang-format to leave if-statements with one statement on the same line.

Example input:

if(is_finished) break;
if(foo) {
    // do something
}

clang-format output:

if(is_finished)
    break;
if(foo) {
    // do something
}

Wanted output:

if(is_finished) break;
if(foo) {
    // do something
}

None of the space related options seem to match this style.

current config:

---
Language: Cpp 
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
SpaceBeforeParens: Never
BraceWrapping:
  AfterControlStatement: false
like image 370
allo Avatar asked Sep 12 '25 02:09

allo


1 Answers

The relevant config option is AllowShortIfStatementsOnASingleLine.

The choices are:

  • Never
  • WithoutElse
  • Always

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

like image 70
sweenish Avatar answered Sep 13 '25 14:09

sweenish