<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/77308>77308</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] Incorrect short statement formatting with Allman (17.06)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dertarr
      </td>
    </tr>
</table>

<pre>
    Hello. Started using `clang-format` version `17.06` and noticed several strange issues. Here is one of them:

Clang formatter handles short statements inappropriately. I merged all issues with short statements in a single bug report. Here is my config:
```
---
Language: Cpp
BasedOnStyle: Google
AlignConsecutiveShortCaseStatements:
  Enabled: true
  AcrossComments:   true
  AcrossEmptyLines: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
BreakBeforeBraces: Allman
InsertBraces: true
...
```

Here is the list of related issues I see:

1. Formatter treats `Allow` word in a misleading way, more like `Enforce`. I would assume that this config *allows* me to keep one-liner short statements where I want to, without complaining about also having short statements on multiple lines. Maybe it makes sense to rename the `AllowShortXxxx` options or to add value `Leave` to `AllowShortBlocksOnASingleLine` option.
2. The `if-else` expression was also shrunk to two lines despite `AllowShortIfStatementsOnASingleLine: WithoutElse` setting.
3. The opening brace for `do {` statement was attached despite `BreakBeforeBraces: Allman` setting.
4. A short one-liner version of `do-while` loop was expanded to multiple lines despite `AllowShortLoopsOnASingleLine: true` setting.

Here is a sample file (on the left) and the formatted file (on the right)
<table>
<tr><th> Sample file </th><th> Formatted output </th></tr>
<tr><td valign="top">

```
void MODULE_ShowShortBlocks(void)
{
  // Short loops on several lines
  while (true)
  {
    continue;
 }

  // Short loops on a single line
  while (true) { continue; }

  // Short case statements on several lines
  switch (a)
 {
    case 1:
      x = 1;
      break;
    case 2:
 return;
  }

  // Short case statements merged
  switch (a)
 {
    case 1: x = 1; break;
    case 2: return;
  }

  // Short 'if' without 'else' block
  if (a == 0) { return; }

  // Short 'if' with 'else' block
  if (z == 7)
  {
    return;
 }
  else
  {
    LogError();
  }

  // A "do" example on a multiple lines
  do
  {
     PutItems(items, 1);
  } while (false);

  // A "do" example on a single line
  do { PutItems(items, 1); } while (false);
}
```

</td><td valign="top">

```
void MODULE_ShowShortBlocks(void)
{
  // Short loops on several lines
  while (true) { continue; }

  // Short loops on a single line
  while (true) { continue; }

  // Short case statements on several lines
  switch (a)
  {
    case 1: x = 1; break;
    case 2: return;
  }

  // Short case statements merged
  switch (a)
  {
    case 1: x = 1; break;
    case 2: return;
  }

  // Short 'if' without 'else' block
  if (a == 0) { return; }

  // Short 'if' with 'else' block
  if (z == 7) { return; }
  else { LogError(); }

  // A "do" example on a multiple lines
  do {
    PutItems(items, 1);
  } while (false);

  // A "do" example on a single line
  do {
    PutItems(items, 1);
  } while (false);
}
```

</td></tr></table>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWNtu47wRfhr6ZmBDphzLufCFj90AWWyBtGjvCloaSWwoUiApO-7TF0NZPifZLBY9AH8Q2BY5M983R4kSzslCI07Zw5w9LHui8aWx0wytF9b2NibbT7-hUmYAL15Yjxk0TuoC2DhKldBFPze2Ep6NI9iiddJo2homg2hMa0JnoI2XKWbgcItWKHDeCl0gSOcadAP4hpYuwGgEk4MvsWLxjEVLFh0-F4QELZJHC6XQmUIHrjTWg_PCY4XaO5Ba1LU1tZXCo9oP4AkqtAVmIJQ6AMJO-vKeKggg1xTCpinAYm2sP7Gr9pAancvixG0cHf7DZb_fb388C100okAWz2BR1-3iXDjMfugXv1dh40_GFArbvZmShV4Y7TBtvNziC3FbCIcvR3pHUICVFhuFGRnxtsFueZZa49zCVJ08wJ39VVX7_bPU6C71Z0qZXcCdK5O-uh969hJiQbIkOlM7sXfXwkTyWWxQ3Srcs_2Unzy6Ufib9KVp_Eq5G71n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