clang-format option to not break lines at operator ->

Hi All,

I am currently trying to beatify a reasonably large code base, and I am quite surprised about how clang-format breaks long lines where there is a ‘->’ operator.

Example:
% cat test.cpp
int func() {
aaaaaaaaaaa = bbbbbbbbbb->cccccccccc(SomeNamespace::Function(dddddddd?“eeeeeeee”:“ffffffff”)).gggggggggg();
}

I tried different clang-format styles, but they all break this long line at the ‘->’:

% for s in LLVM Google Chromium Mozilla WebKit; do clang-format -style=“{BasedOnStyle: $s, ColumnLimit: 80}” test.cpp; done
int func() {
aaaaaaaaaaa = bbbbbbbbbb
->cccccccccc(SomeNamespace::Function(dddddddd ? “eeeeeeee”
: “ffffffff”))
.gggggggggg();
}
int func() {
aaaaaaaaaaa = bbbbbbbbbb
->cccccccccc(SomeNamespace::Function(dddddddd ? “eeeeeeee”
: “ffffffff”))
.gggggggggg();
}
int func() {
aaaaaaaaaaa = bbbbbbbbbb
->cccccccccc(SomeNamespace::Function(dddddddd ? “eeeeeeee”
: “ffffffff”))
.gggggggggg();
}
int
func()
{
aaaaaaaaaaa =
bbbbbbbbbb
->cccccccccc(SomeNamespace::Function(dddddddd ? “eeeeeeee” : “ffffffff”))
.gggggggggg();
}
int func()
{
aaaaaaaaaaa = bbbbbbbbbb
->cccccccccc(SomeNamespace::Function(
dddddddd ? “eeeeeeee” : “ffffffff”))
.gggggggggg();
}

Is there a way to prevent clang-format from breaking lines like that at the ‘->’?

Thank you,
Mikhail

Hi Mikhail,

Hi Csaba,

I can think of several possible results that look better (to me) than the current one:

int func() {
aaaaaaaaaaa = bbbbbbbbbb->cccccccccc(
SomeNamespace::Function(dddddddd?“eeeeeeee”:“ffffffff”)).gggggggggg();
}

OR

int func() {
aaaaaaaaaaa = bbbbbbbbbb->cccccccccc(
SomeNamespace::Function(
dddddddd?“eeeeeeee”:“ffffffff”)
).gggggggggg();
}

OR

int func() {
aaaaaaaaaaa = bbbbbbbbbb->cccccccccc(SomeNamespace::Function(
dddddddd?“eeeeeeee”:“ffffffff”)).gggggggggg();
}

In fact, pretty much anything that doesn’t break the line at ‘->’ works for me.

This is not the case with the current clang-format, though. If you run clang-format (with any predefined style) for either of the aforementioned codes, it always breaks the line with ‘->’, even if all lines satisfy the column limit.

Thank you,
Mikhail