llvm / llvm/llvm-project

Bad formatting of lambda expression with trailing return type

Open
#179,549 1 comment 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

This:

```cpp
#include

using T = std::optional;

int main() {
auto foo = []() -> T {
return T{1};
}().value();
}
```

gets formatted to:

```cpp
#include

using T = std::optional;

int main() {
auto foo = []() -> T {
return T{1};
}()
.value();
}
```

Which seems like a bug (why is the `.value()` part there?).

On the other hand this (note: trailing return type omitted):

```cpp
#include

using T = std::optional;

int main() {
auto foo = []() {
return T{1};
}().value();
}
```

gets formatted to:

```cpp
#include

using T = std::optional;

int main() {
auto foo =
[]() {
return T{1};
}()
.value();
}
```

which is much more reasonable.

Config file:

```
---
Language: Cpp
Standard: Auto
AllowShortLambdasOnASingleLine: None
```

`clang-format` version: 21.1.7

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.