Brace rules for single line else/multi-line if

Hi all,

I wanted to ask about what seems like an implied coding standard that is not followed in the code in all places:

LLVM Coding Standards — LLVM 20.0.0git documentation says " For example, readability is also harmed if an if /else chain does not use braced bodies for either all or none of its members".

I see there was a big thread about this a while back but came to no conclusion.

Codifying our Brace rules- - Project Infrastructure / LLVM Dev List Archives - LLVM Discussion Forums

I do find reading code like this:

  if (EllipsisLines < StringRef(ElidedLines).count('\n')) {
    for (unsigned i = 0; i < EllipsisLines; ++i) {
      WithColor(OS, raw_ostream::BLACK, /*Bold=*/true)
          << right_justify(".", LabelWidth);
      OS << '\n';
    }
  } else
    OS << ElidedLines;

or

    if (isa<WsloopOp>(nested)) {
      if (!llvm::dyn_cast_if_present<ParallelOp>((*this)->getParentOp()))
        return emitError() << "an 'omp.wsloop' nested wrapper is only allowed "
                              "when 'omp.parallel' is the direct parent";
    } else if (!isa<SimdOp>(nested))
      return emitError() << "only supported nested wrappers are 'omp.simd' and "
                            "'omp.wsloop'";

difficult and it seems the coding standard implies that the else/else if here should have a brace because the if has it. Since the earlier thread had not reached any conclusion, I am proposing we just make this case clearer by adding another example in the coding standard section for this case. There is already:

// Use braces for the `if` block to keep it uniform with the `else` block.
if (isa<FunctionDecl>(D)) {
  handleFunctionDecl(D);
} else {
  // In this `else` case, it is necessary that we explain the situation with
  // this surprisingly long comment, so it would be unclear without the braces
  // whether the following statement is in the scope of the `if`.
  handleOtherDecl(D);
}

we will just add a complementary example:

// Use braces for the `else` block to keep it uniform with the `if` block.
if (isa<FunctionDecl>(D)) {
  verifyFunctionDecl(D);
  handleFunctionDecl(D);
} else {
  handleOtherDecl(D);
}

This rule seems to be violated quite a bit in the LLVM codebase: git grep "} else$" | wc -l gives 2706. and git grep "} else if (.*)$" | wc -l gives 741. But the additional example will atleast clarify it (assuming I am reading the coding standard correctly).

1 Like

Yes, I recall that thread :slight_smile:

I still consider our documentation to SAY what you’re suggesting (but as a suggestion, rather than mandatory), and I think we should do it.

We’ve discussed this a few times, and that statement is as strong as I’ve been able to get into the docs (I might have actually written that line?).

Yeah, @erichkeane you documented this rule in the first place. The particular line I quoted seems was added by @mehdi_amini as a part of

Clarify a bit the guideline on omitting braces, including more exampl… · llvm/llvm-project@140c296 (github.com)

Can this be encoded into a clang-tidy rule?

Thanks to muscle memory from other projects, I am constantly forgetting it when writing new code. Thankfully, reviewers usually point it out :slightly_smiling_face:

+1. In the earlier thread, it was suggested if clang-format could do this, clang-tidy seems reasonable as well.

Here’s what I am suggesting as a PR: [NFC][CodingStandard] Add additional example for if-else brace rule by jurahul · Pull Request #111733 · llvm/llvm-project (github.com)

Note, my intent is to commit the PR above in a week or so if I don’t hear any objections (it has @erichkeane and @mehdi_amini 's LGTM).

+1. My eye definitely finds it easier to read a long if/else when the clauses are fully braced. This is distinctly higher value than saving one line of vertical space. IMO the only exception should be an if/else chain where all bodies are single-line.

clang-format already got the RemoveBracesLLVM option, and we’ve been using it (together with InsertBraces) to format clang-format sources since version 14.

To close out on this, I have added additional example for both single line else and else if needing braces to the coding standard doc.

1 Like