llvm / llvm/llvm-project

clang-format: Allow empty records on a single line

Open
#166,632 5 comments 0 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Feel free to close this if this is already possible and I just failed to figure out how.

There doesn't appear to be a way to accomplish this with clang-format:

```cpp
// Break before braces after class/struct/union:
struct Foo
{
int x;
};

// AND allow empty class/struct/union on a single line:
struct Foo {};
```

If we set `BraceWrapping: AfterStruct: true` and `BraceWrapping: SplitEmptyRecord: false`, we instead get this:

```cpp
struct Foo
{};
```

My proposal would be to add a `bool AllowEmptyRecordsOnASingleLine` option that handles this case similarly to the `AllowShortEnumsOnASingleLine` option. If others think this would be an acceptable enhancement, I can look into implementing it myself, but I thought I'd request feedback before I put in the work.

Contributor guide

Open the contributing guide

Research direction

Start by locating clang-format's handling of BraceWrapping, SplitEmptyRecord, and AllowShortEnumsOnASingleLine. Check the existing formatting tests for records and empty constructs; done means non-empty records still break before braces while empty records format as `struct Foo {};`.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.