Misleading indentation on if statements with clang-format AlignAfterOpenBracket (original) (raw)
December 8, 2025, 5:54am 1
I’m using a combination of clang-format options that causes a really misleading indentation on long if-statement conditions. Here’s a small example:
.clang-format:
AlignAfterOpenBracket: Align
IndentWidth: 4
ContinuationIndentWidth: 8
Input:
void foo() {
if (long_condition_aaaaaaaaaaaaaaaaaaaaaaaaaaaaa() && long_condition_bbbbbbbbbbbbbbbbbbbbbbbbbbbbb()) {
bar();
}
}
Output:
void foo() {
if (long_condition_aaaaaaaaaaaaaaaaaaaaaaaaaaaaa() &&
long_condition_bbbbbbbbbbbbbbbbbbbbbbbbbbbbb()) {
bar();
}
}
The problem is that aligning the second line of the condition with the open paren happens to make it also line up exactly with the body of the if statement, so it looks like the condition is actually part of the body. for loops have basically the same problem - it’s off by one space but I still find it to be confusing at a glance.
So I have two questions. First, can anyone recommend a config change that would avoid this problem? I don’t want to disable AlignAfterOpenBracket because I think it dramatically improves readability in other cases. I also can’t change the indent level.
Second question: would there be any appetite for a patch to fix this problem, one way or another? I have a patch to the clang-7 era clang-format that I’ve been using for years. It works but it’s a bit of a weird special case. I’d be open to other approaches. Maybe another option to control whether AlignAfterOpenBracket is applied to specific kinds of control statements (I only care about if and for, but while statements would have almost the same problem with 8-space indents).
Thanks!
If you were formatting it by hand, what would you do?
Without forcing the { onto its own line as well, I feel like inserting a // comment might be a way to do it? Or even just a blank line?
bmoody December 8, 2025, 9:32pm 3
The formatting that I want is
void foo() {
if (long_condition_aaaaaaaaaaaaaaaaaaaaaaaaaaaaa() &&
long_condition_bbbbbbbbbbbbbbbbbbbbbbbbbbbbb()) {
bar();
}
}
Which is what I get with AlignAfterOpenBracket: DontAlign. It uses the ContinuationIndentWidth of 8. This is also what my patched clang-format is doing.
But I don’t want to disable AlignAfterOpenBracket across the board because it’s so helpful in other cases, particularly function param lists and call argument lists.