Skip to content

[clang-format] AlignAfterOpenBracket and Cpp11BracedListStyle option combination leads to inconsistent formatting #136369

Open
@mrizaln

Description

@mrizaln

Given a C++ file with following content

struct S {
  int a;
  int b;
};

int aaa(S s);
int bbbb(S s);

namespace f {
int a(S s);
int bb(S s);
} // namespace f

int main() {
  auto a = S{ 1, 2 };
  aaa({ 1, 2 });
  bbbb({ 1, 2 });
  f::a({ 1, 2 });
  f::bb({ 1, 2 });

  aaa({
      1,
      2,
  });

  auto i = aaa({
      .a = 1,
      .b = 2,
  });

  bbbb({
      1,
      2,
  });

  auto j = bbbb({
      .a = 1,
      .b = 2,
  });

  f::a({
      1,
      2,
  });

  auto k = f::a({
      .a = 1,
      .b = 2,
  });

  f::bb({
      1,
      2,
  });

  auto l = f::bb({
      .a = 1,
      .b = 2,
  });
}

when it is formatted using clang-format (20.1.2) with the following config file,

AlignAfterOpenBracket: BlockIndent
Cpp11BracedListStyle: false

the result of the formatting is inconsistent:

struct S {
  int a;
  int b;
};

int aaa(S s);
int bbbb(S s);

namespace f {
int a(S s);
int bb(S s);
} // namespace f

int main() {
  auto a = S{ 1, 2 };
  aaa({ 1, 2 });
  bbbb({ 1, 2 });
  f::a({ 1, 2 });
  f::bb({ 1, 2 });

  aaa({
      1,
      2,
  });

  auto i =
      aaa({
          .a = 1,
          .b = 2,
      });

  bbbb(
      {
          1,
          2,
      }
  );

  auto j = bbbb(
      {
          .a = 1,
          .b = 2,
      }
  );

  f::a(
      {
          1,
          2,
      }
  );

  auto k = f::a(
      {
          .a = 1,
          .b = 2,
      }
  );

  f::bb(
      {
          1,
          2,
      }
  );

  auto l = f::bb(
      {
          .a = 1,
          .b = 2,
      }
  );
}

The bug only happen specifically when setting AlignAfterOpenBracket to BlockIndent and Cpp11BracedListStyle to false, also happen when setting AlignAfterOpenBracket to AlwaysBreak and Cpp11BracedListStyle to false, though the result is different:

struct S {
  int a;
  int b;
};

int aaa(S s);
int bbbb(S s);

namespace f {
int a(S s);
int bb(S s);
} // namespace f

int main() {
  auto a = S{ 1, 2 };
  aaa({ 1, 2 });
  bbbb({ 1, 2 });
  f::a({ 1, 2 });
  f::bb({ 1, 2 });

  aaa({
      1,
      2,
  });

  auto i =
      aaa({
          .a = 1,
          .b = 2,
      });

  bbbb(
      {
          1,
          2,
      });

  auto j = bbbb(
      {
          .a = 1,
          .b = 2,
      });

  f::a(
      {
          1,
          2,
      });

  auto k = f::a(
      {
          .a = 1,
          .b = 2,
      });

  f::bb(
      {
          1,
          2,
      });

  auto l = f::bb(
      {
          .a = 1,
          .b = 2,
      });
}

The expected result should be the same as the input.
Tested on clang-format 20.1.2 (Fedora 20.1.2-3.fc42).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions