<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57609>57609</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-format: AllowShortCaseLabelsOnASingleLine only for simple cases
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AdamBucior
</td>
</tr>
</table>
<pre>
Hi,
I would like clang-format to put short case labels on single line only for simple single-case cases like:
```
switch (i) {
case 0: return 3;
case 1: return 7;
case 2: return 1;
case 3: return 5;
}
```
But not for more complicated multiple-case cases like:
```
switch (i) {
case 0:
case 1:
return i;
case 2:
case 3:
i *= 2;
break;
}
```
Which `clang-format -style="{AllowShortCaseLabelsOnASingleLine: true}"` formats as:
```
switch (i) {
case 0:
case 1: return i;
case 2:
case 3: i *= 2; break;
}
```
which doesn't make it obvious that `return i;` is also done in case `i` is 0 (and similarly for `case 2:`).
The problem also exists for fallthrough cases like:
```
switch (i) {
case 0:
g();
[[fallthrough]];
case 1:
f();
break;
}
```
Which `clang-format -style="{AllowShortCaseLabelsOnASingleLine: true}"` formats as:
```
switch (i) {
case 0: g(); [[fallthrough]];
case 1: f(); break;
}
```
I propose to add a new option: `AllowShortCaseLabelsOnASingleLine: SingleCases`
The last example might be more challenging because `clang-format` would need a way to detect whether the previous case can fallthrough. I see 2 ways:
- Just check if there is a `[[fallthrough]]` attribute (this option would cause wrong formatting if someone forgets `[[fallthrough]]`)
- Check for `return` or `break` (this option might be harder to implement correctly)
I'm fine with both ways.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzdVsGOozgQ_Rq4lBI50EDnwCHpaLS9GmkPM9KcDVTA2wZHtpls_n6rMOkh3TO7WalPKyEiu1xVr169MqlMcyl_U1HyFIlDJHbh_QxnM-oGtHpBqLUc2tXR2F568AZOowfXGeuhlg5Bywq1AzOAU0OraUMNSEt9AfKh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