llvm / llvm/llvm-project

[clang-format] Trailing comma breaks formatting in nested braced initializer

Open
#183,091 2 comments 0 reactions 0 assignees View on GitHub
clang-format confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

clang-format version: 21.1.5
OS: Windows 11

.clang-format:
```yaml
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4

AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AlignEscapedNewlines: Left

InsertBraces: true
BreakAfterReturnType: None
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllArgumentsOnNextLine: false
AlignAfterOpenBracket: Align
PenaltyReturnTypeOnItsOwnLine: 1000
PenaltyBreakAssignment: 500
BreakBeforeBinaryOperators: NonAssignment

AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
IncludeCategories:
- Regex: '^".*'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentWidth: 4
UseTab: Never
InsertNewlineAtEOF: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
SpaceInEmptyParentheses: false
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
TabWidth: 4

ReflowComments: true
SortUsingDeclarations: true
Standard: c++20
...
```

When formatting code containing a nested braced initializer, adding a trailing comma inside the inner initializer causes clang-format to stop reformatting that section(only indent of first line works).

Example:

```cpp
mTextureCopies . push_back({ .dstTexture = &dst, .srcOffset = static_cast(mCurrentOffset),
.extent = { .width = dst.get_size().x, .height = dst.get_size().y, .depth = 1, },.generateMips = generateMips,
});
```

After removing the trailing comma inside the nested initializer:

```cpp
mTextureCopies.push_back({
.dstTexture = &dst,
.srcOffset = static_cast(mCurrentOffset),
.extent = { .width = dst.get_size().x, .height = dst.get_size().y, .depth = 1 },
.generateMips = generateMips,
});
```

The only difference is the trailing comma after `.depth = 1`.

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.