llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] `BreakAfterOpenBracketIf` is ignored unless `ContinuationIndentWidth` is also set to a value smaller than `IndentWidth * 2`

Open arthur-cornfield opened this issue 4 months ago • 2 comments

.clang-format:

BinPackArguments: false
BreakAfterOpenBracketIf: true
BreakBeforeCloseBracketIf: true

Demo C file:

#include <stdio.h>

int main() {
  if (printf("%s %s %s %s %s %s", "foobar", "foobar", "foobar", "foobar", "foobar", "foobar") < 0) {
    return 1;
  }
}

Expected formatting:

#include <stdio.h>

int main() {
  if (
      printf("%s %s %s %s %s %s",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar") < 0
  ) {
    return 1;
  }
}

(i.e., newline should be inserted after opening parenthesis of the if statement.)

Actual formatting:

#include <stdio.h>

int main() {
  if (printf("%s %s %s %s %s %s",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar") < 0) {
    return 1;
  }
}

If ContinuationIndentWidth rule is set to a value less than IndentWidth multiplied by 2 (like 3 or 2) then the formatting is as expected. I tried changing various Penalty* rules but it didn't help.

clang-format version is 22.1.8

arthur-cornfield avatar Jul 27 '26 08:07 arthur-cornfield

@llvm/issue-subscribers-clang-format

Author: arthur-cornfield

.clang-format: ```yaml BinPackArguments: false BreakAfterOpenBracketIf: true BreakBeforeCloseBracketIf: true ```

Demo C file:

#include &lt;stdio.h&gt;

int main() {
  if (printf("%s %s %s %s %s %s", "foobar", "foobar", "foobar", "foobar", "foobar", "foobar") &lt; 0) {
    return 1;
  }
}

Expected formatting:

#include &lt;stdio.h&gt;

int main() {
  if (
      printf("%s %s %s %s %s %s",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar") &lt; 0
  ) {
    return 1;
  }
}

(i.e., newline should be inserted after opening parenthesis of the if statement.)

Actual formatting:

#include &lt;stdio.h&gt;

int main() {
  if (printf("%s %s %s %s %s %s",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar",
             "foobar") &lt; 0) {
    return 1;
  }
}

If ContinuationIndentWidth rule is set to a value less than IndentWidth multiplied by 2 (like 3 or 2) then the formatting is as expected. I tried changing various Penalty* rules but it didn't help.

Hi, I would like to work on this issue. Could you assign it to me? Thank you!

ChrisLee02 avatar Aug 29 '26 04:08 ChrisLee02