llvm / llvm/llvm-project

[clang-format] incorrectly inserts newline and changes indentation in designated initializers

Open
#172,804 1 comment 2 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**Description:**
When using clang-format with Google style, the formatter unexpectedly inserts a newline between `.x =` and `{` in designated initializers, and changes the indentation from 2 spaces to 4 spaces. This behavior seems inconsistent with the expected formatting.

**clang-format version:**
clang-format version 21.1.6 (https://github.com/llvm/llvm-project a832a5222e489298337fbb5876f8dcaf072c5cca)

**Configuration:**
```bash
-style='{ Language: Cpp, BasedOnStyle: Google }'
```

**Input code:**
```c
struct T {
struct {
int y;
int z;
} x;
};

struct T t = {
.x = {
.y = 0,
.z = 1,
},
};
```

**Actual output after formatting:**
```c
struct T {
struct {
int y;
int z;
} x;
};

struct T t = {
.x =
{
.y = 0,
.z = 1,
},
};
```

**Expected behavior:**
The formatting should either maintain the original code structure or follow a more reasonable formatting pattern. The current output introduces unnecessary line breaks and inconsistent indentation.

Interestingly, when adding `BreakBeforeBinaryOperators: All` to the configuration, the code maintains its original format:

**Working configuration:**
```bash
-style='{ Language: Cpp, BasedOnStyle: Google, BreakBeforeBinaryOperators: All }'
```

**Output with working configuration (maintains original format):**
```c
struct T {
struct {
int y;
int z;
} x;
};

struct T t = {
.x = {
.y = 0,
.z = 1,
},
};
```

The issue appears to be related to how clang-format handles the `=` operator in designated initializers when using the Google style base.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.