llvm / llvm/llvm-project

[clang-format] Wrongly formats a Java line that contains `record` keyword

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

Description

With code style for braces on a new line, disabling clang-format or removing the `record` keyword helps:
```
// clang-format off
private record PathEntry(CountryItem item, boolean myMapsMode, int topPosition, int topOffset)
{
@Override
public String toString()
{
return item.id + " (" + item.name + "), " + "myMapsMode: " + myMapsMode + ", topPosition: "
+ topPosition + ", topOffset: " + topOffset;
}
}
// clang-format on
```

Otherwise, it formats like this:
```
private record PathEntry(CountryItem item, boolean myMapsMode, int topPosition, int topOffset) {
@Override
public String toString()
{
return item.id + " (" + item.name + "), " + "myMapsMode: " + myMapsMode + ", topPosition: "
+ topPosition + ", topOffset: " + topOffset;
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by running clang-format on the Java snippet in the issue with braces configured on a new line, then compare the output with the expected formatting shown. Trace the handling of Java record declarations and add a regression test demonstrating that the brace style is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.