Skip to content

[clang-format] 'else if' clause not broken when using AlignAfterOpenBracket: BlockIndent #54663

Closed
@yseymour

Description

@yseymour

clang-format version:
Debian clang-format version 14.0.0-++20220329040358+3f43d803382d-1~exp1~20220329160446.105

Code:

void foo() {
        if (thisIsRatherALongIfClause && thatIExpectToBeBroken || ontoMultipleLines && whenFormattedCorrectly) {
        }

        if (false) {
        }
        else if (thisIsRatherALongIfClause && thatIExpectToBeBroken || ontoMultipleLines && whenFormattedCorrectly) {
        }
}

Output with defaults:

❯ clang-format --style='{ColumnLimit: 60}' tmp.cc
void foo() {
  if (thisIsRatherALongIfClause && thatIExpectToBeBroken ||
      ontoMultipleLines && whenFormattedCorrectly) {
  }

  if (false) {
  } else if (thisIsRatherALongIfClause &&
                 thatIExpectToBeBroken ||
             ontoMultipleLines && whenFormattedCorrectly) {
  }
}

Output with AlignAfterOpenBracket: BlockIndent

❯ clang-format --style='{ColumnLimit: 60, AlignAfterOpenBracket: BlockIndent}' tmp.cc
void foo() {
  if (thisIsRatherALongIfClause && thatIExpectToBeBroken ||
      ontoMultipleLines && whenFormattedCorrectly) {
  }

  if (false) {
  } else if (thisIsRatherALongIfClause && thatIExpectToBeBroken || ontoMultipleLines && whenFormattedCorrectly) {
  }
}

Expected output:
First of all, I was intuitively expecting BlockIndent to break the if/else-if clause in a similar way to a function declaration:

void foo(
    int veryLongArgumentName1, int veryLongArgumentName2,
    int veryLongArgumentName3
) {}

and my hope was that there would be a BinPack... option to place each part of the clause on each line just as one can with arguments and parameters, but that doesn't seem to be the case.

If that's not possible and/or it's not a design goal for that option, then at the least I'd expect the else if clause to be broken, because currently it doesn't get wrapped at all when the BlockIndent alignment style is used, which seems buggy.

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