| Issue |
237693
|
| Summary |
[clang-tidy] modernize-use-trailing-return-type: lambda fix-it writes '[] -> T' without '()', which is a C++23 extension before C++23
|
| Labels |
clang-tidy
|
| Assignees |
|
| Reporter |
YevheniiKotyrlo
|
With `TransformLambdas` at its default (`all`), `modernize-use-trailing-return-type` fixes a lambda that has no parameter list by inserting ` -> T` before the body, so `[] { return 1; }` becomes `[] -> int { return 1; }`. Leaving out the `()` before a trailing return type is only valid from C++23 on (P1102R2). Under `-std=c++20` and earlier clang warns `lambda without a parameter clause is a C++23 extension [-Wc++23-extensions]`, so a build with `-Werror` or `-pedantic-errors` breaks after `--fix`, and other compilers in pre-C++23 modes may reject it outright.
`diagOnLambda` in `clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp` builds the insertion as `" -> " + ReturnType.getAsString(...)` without checking `Lambda->hasExplicitParameters()`. I'd expect it to insert `() -> int` when the lambda has no parameter clause and the language mode is before C++23.
## Steps to reproduce
Reproducer: https://gist.github.com/YevheniiKotyrlo/9c0afae046565e454cc6824b02235028
```cpp
// lambda.cpp
int count() {
auto _one_ = [] { return 1; };
return one();
}
```
```console
$ git clone https://gist.github.com/9c0afae046565e454cc6824b02235028.git trailing-lambda
$ cd trailing-lambda
$ clang-tidy --checks='-*,modernize-use-trailing-return-type' --fix lambda.cpp -- -std=c++20
...
lambda.cpp:2:14: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
2 | auto _one_ = [] { return 1; };
| ^
| -> int
...
clang-tidy applied 3 of 3 suggested fixes.
$ cat lambda.cpp
auto count() -> int {
auto _one_ = [] -> int { return 1; };
return one();
}
$ clang++ -std=c++20 -pedantic-errors -fsyntax-only lambda.cpp
lambda.cpp:2:17: error: lambda without a parameter clause is a C++23 extension [-Werror,-Wc++23-extensions]
2 | auto _one_ = [] -> int { return 1; };
| ^
| ()
1 error generated.
```
Without `-pedantic-errors` the same line is a `-Wc++23-extensions` warning.
Expected: under `-std=c++20` the fix writes `[]() -> int { return 1; }`, which is valid C++11 through C++23.
Actual: it writes `[] -> int { return 1; }`.
The same fix also spells the deduced type as it was written where it was declared, so a lambda returning `outer::inner::make()` (declared as returning `Widget` inside `namespace outer::inner`) gets `-> Widget`, which doesn't compile outside that namespace; I think that part is #144509.
## Versions
- clang-tidy and clang built from `main` at 2b54d70a764a61c2565f8a7f9890c9f71c60d9b3 (MSVC 14.51, Release); 23.1.3 behaves the same
- Windows 11 (10.0.26300); nothing here is platform specific
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs