[PATCH] D100727: [clang-format] Correctly apply AllowShortIfStatementsOnASingleLine: Always to else branch.

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 19 06:27:06 PDT 2021


curdeius planned changes to this revision.
curdeius added a subscriber: klimek.
curdeius added a comment.

More I look at the current state of this option, more I think it's misleading.
I would expect that the if after else is also controlled by this option. And, the last else as well (adding yet another option for that seems to be a bit an overkill)

@klimek, @mydeveloperday, I'd love to hear your input as well on the best course of action, as I've seen that you've discussed this topic a bit in https://reviews.llvm.org/D59087.

The current status quo looks like:

  /// Different styles for handling short if statements.
  enum ShortIfStyle : unsigned char {
    /// Never put short ifs on the same line.
    /// \code
    ///   if (a)
    ///     return;
    ///
    ///   if (b)
    ///     return;
    ///   else
    ///     return;
    ///
    ///   if (c)
    ///     return;
    ///   else {
    ///     return;
    ///   }
    /// \endcode
    SIS_Never,
    /// Put short ifs on the same line only if there is no else statement.
    /// \code
    ///   if (a) return;
    ///
    ///   if (b)
    ///     return;
    ///   else
    ///     return;
    ///
    ///   if (c)
    ///     return;
    ///   else {
    ///     return;
    ///   }
    /// \endcode
    SIS_WithoutElse,
    /// Always put short ifs on the same line.
    /// \code
    ///   if (a) return;
    ///
    ///   if (b) return;
    ///   else
    ///     return;
    ///
    ///   if (c) return;
    ///   else {
    ///     return;
    ///   }
    /// \endcode
    SIS_Always,
  };

I.e. `WithoutElse` does not depend on the else statement being compound or not. I think I'll push a NFC commit to fix documentation and add tests for this.

Anyway, what I'm inclined to do is to have these options:

- Never (same as now)
- WithoutElse (same as now, concerns only a simple `if (c) f();`)
- OnlyFirstIf (renamed from `Always`, and `Always` kept for backward compatibility, it would behave as currently, so only the first `if` in the sequence of if else if else if else is concerned)
- AllIfsAndElse (would do what I want to achieve in this patch, so format like this:

  if (c) f();
  else if (c) f();
  else if (c) f();
  else if();

Naming is hard. All suggestions are welcome :).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D100727/new/

https://reviews.llvm.org/D100727



More information about the cfe-commits mailing list