Skip to content

clang-format RemoveBracesLLVM should handle else block containing an if block #55663

@owenca

Description

@owenca

The RemoveBracesLLVM option doesn't remove the braces of an else block that contains a single if statement when the body of the if statement is a block:

$ cat test.cpp
void f() {
  if (a1)
    f1();
  else {
    if (a2)
      f2();
  }

  if (a3)
    f3();
  else {
    if (b) {
      g();
      h();
    }
  }
}
$ clang-format -version
clang-format version 15.0.0 (https://github.com/llvm/llvm-project 09865ae95dbf0322bbf3e7b3847b2b11373a0297)
$ clang-format -style="{InsertBraces: true, RemoveBracesLLVM: true}" test.cpp
void f() {
  if (a1)
    f1();
  else if (a2)
    f2();

  if (a3) {
    f3();
  } else {
    if (b) {
      g();
      h();
    }
  }
}

Expected output:

...
  if (a3) {
    f3();
  } else if (b) {
    g();
    h();
  }
...

Metadata

Metadata

Assignees

Labels

clang-formatenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions