[clang-tools-extra] [clang-tidy] Fix use-trailing-return-type insert location for lambdas (PR #240117)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 02:57:46 PST 2026
https://github.com/awrrrr created https://github.com/llvm/llvm-project/pull/240117
Depends on #240116. GitHub shows that PR's commit here too, so please review only the last commit, "[clang-tidy] Fix use-trailing-return-type insert location for lambdas". I'll rebase this onto main once #240116 lands.
`modernize-use-trailing-return-type` inserted a lambda's trailing return type one character before the body or `requires` clause. When nothing separated the body from the preceding token, that split the token. When the body came from a macro, the check could crash.
| Input | Before | After |
|---|---|---|
| `[](){ return 1; }` | `[]( -> int){ return 1; }` | `[]() -> int{ return 1; }` |
| `[](int i){ return i; }` | `[](int i -> int){ return i; }` | `[](int i) -> int{ return i; }` |
| `[x]() mutable{ return x; }` | `[x]() mutabl -> inte{ return x; }` | `[x]() mutable -> int{ return x; }` |
| `[]{ return 1; }` (C++23) | `[ -> int]{ return 1; }` | `[] -> int{ return 1; }` |
| `[]() /*c*/{ return 1; }` | `[]() /*c* -> int/{ return 1; }` | `[]() -> int /*c*/{ return 1; }` |
| `[]() // c` + newline + `{ return 1; }` | `-> int` lost inside the comment | `[]() -> int // c` |
| `[](int i)requires true { ... }` | `[](int i -> auto)requires true` | `[](int i) -> auto requires true` |
| `#define BODY { return 1; }` then `[] BODY` | assertion `isExpansion() && "Not a macro expansion SLocEntry!"` | warning, no fix |
The crash happened when the body's macro was the first macro expansion in the file: the location one character before the body then belonged to the file instead of an expansion.
The check now inserts the return type after the token before the body or `requires` clause, skipping comments, and adds a space if `requires` follows directly. If the body comes from a macro, it keeps the warning but doesn't offer a fix, unless the lambda is written inside a macro argument, such as `ID([](){ return 1; })`, where the fix still applies.
Because the return type now always goes right after the previous token, two existing expectations change:
- In `use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp`, C++23: `[&x] /* comment */ -> int` becomes `[&x] -> int /* comment */`, matching the pre-C++23 fix `[&x]() -> int /* comment */`.
- In `use-trailing-return-type-transform-lambdas-cxx20.cpp`, a `requires` clause on its own line now gets `noexcept -> auto` at the end of the line above, instead of `-> auto requires ...` on its own line.
Known limitation: if a preprocessor directive sits between the parameter list and the body, the return type now lands after the directive, for example `#endif -> int`, and is ignored. Before this change that case only worked when the body was indented.
Tests: the new `use-trailing-return-type-transform-lambdas-insert-location.cpp` covers C++11 through C++23. It failed on the old code, including the assertion, which needs the macro case to stay the first expansion in the file.
I used Claude Code to help write this change, and I have reviewed all of it myself.
Assisted-by: Claude Code
🤖 Generated with [Claude Code](https://claude.com/claude-code)
>From 18ff74ae31ad834f12bf73edcbf6d853c7329de1 Mon Sep 17 00:00:00 2001
From: awrrrr <aryanroka360 at gmail.com>
Date: Wed, 4 Nov 2026 20:53:13 +0530
Subject: [PATCH 1/2] [clang-tidy] Add parameter clause in
use-trailing-return-type lambda fixes
Omitting the parameter clause before a trailing return type is only valid
since C++23, but modernize-use-trailing-return-type rewrote `[] { return 1; }`
to `[] -> int { return 1; }` in all language modes.
Before C++23, insert `() -> T` after the lambda introducer or template
parameter list when the body follows directly. If specifiers, a requires
clause or a macro come first, keep the warning but don't offer a fix.
Fixes #237693
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
.../modernize/UseTrailingReturnTypeCheck.cpp | 38 ++++++++++++
clang-tools-extra/docs/ReleaseNotes.md | 5 ++
...-transform-lambdas-no-parameter-clause.cpp | 62 +++++++++++++++++++
...trailing-return-type-transform-lambdas.cpp | 12 ++--
4 files changed, 109 insertions(+), 8 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
index b4feecbd22a10c..15246330b20e9e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
@@ -405,6 +405,27 @@ static SourceLocation findLambdaTrailingReturnInsertLoc(
return {};
}
+// Returns where a parameter clause can be added to a lambda without one, or an
+// invalid location if specifiers or a requires clause come before the body.
+static SourceLocation
+findLambdaParameterClauseInsertLoc(const LambdaExpr *Lambda,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ const TemplateParameterList *TemplateParams =
+ Lambda->getTemplateParameterList();
+ const SourceLocation LastTokenLoc =
+ TemplateParams ? TemplateParams->getRAngleLoc()
+ : Lambda->getIntroducerRange().getEnd();
+
+ const std::optional<Token> NextToken =
+ Lexer::findNextToken(LastTokenLoc, SM, LangOpts);
+ if (!NextToken ||
+ NextToken->getLocation() != Lambda->getBody()->getBeginLoc())
+ return {};
+
+ return Lexer::getLocForEndOfToken(LastTokenLoc, 0, SM, LangOpts);
+}
+
static void keepSpecifiers(std::string &ReturnType, std::string &Auto,
SourceRange ReturnTypeCVRange, const FunctionDecl &F,
const FriendDecl *Fr, const ASTContext &Ctx,
@@ -627,6 +648,23 @@ void UseTrailingReturnTypeCheck::diagOnLambda(
TransformLambdas == TransformLambda::AllExceptAuto)
return;
+ // Omitting the parameter clause before a trailing return type is only valid
+ // since C++23.
+ if (!Lambda->hasExplicitParameters() && !getLangOpts().CPlusPlus23) {
+ const SourceLocation ParameterClauseInsertLoc =
+ findLambdaParameterClauseInsertLoc(Lambda, *Result.SourceManager,
+ getLangOpts());
+ if (ParameterClauseInsertLoc.isValid())
+ diag(Lambda->getBeginLoc(), "use a trailing return type for this lambda")
+ << FixItHint::CreateInsertion(
+ ParameterClauseInsertLoc,
+ "() -> " + ReturnType.getAsString(
+ Result.Context->getPrintingPolicy()));
+ else
+ diag(Lambda->getBeginLoc(), ErrorMessageOnLambda);
+ return;
+ }
+
const SourceLocation TrailingReturnInsertLoc =
findLambdaTrailingReturnInsertLoc(Method, *Result.SourceManager,
getLangOpts(), *Result.Context);
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 679ca9ecbb4a39..c91ca2ea6c4bfd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -329,6 +329,11 @@ infrastructure are described first, followed by tool-specific sections.
- Kept diagnostics but suppressed unsafe fix-its when no safe
result-preserving rewrite is available.
+- Improved {doc}`modernize-use-trailing-return-type
+ <clang-tidy/checks/modernize/use-trailing-return-type>` check by adding `()`
+ to fixes for lambdas without a parameter clause before C++23, and not offering
+ a fix when that would need specifiers to be moved.
+
- Improved {doc}`performance-inefficient-algorithm
<clang-tidy/checks/performance/inefficient-algorithm>` check to no longer
produce a fix with the container or the searched-for value missing, such as
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
new file mode 100644
index 00000000000000..3a78c6581bd1a4
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
@@ -0,0 +1,62 @@
+// RUN: %check_clang_tidy -check-suffix=BEFORE-CXX23 -std=c++11,c++14,c++17 %s modernize-use-trailing-return-type %t -- -- -Wno-c++23-extensions
+// RUN: %check_clang_tidy -check-suffixes=BEFORE-CXX23,CXX20 -std=c++20 %s modernize-use-trailing-return-type %t -- -- -Wno-c++23-extensions
+// RUN: %check_clang_tidy -check-suffixes=CXX23 -std=c++23-or-later %s modernize-use-trailing-return-type %t
+
+// A lambda can only omit its parameter clause before a trailing return type
+// since C++23 (P1102R2), so the fix has to add '()' in earlier language modes.
+
+#define LAMBDA_BODY { return 1; }
+
+void test_no_parameter_clause() {
+ int x = 0;
+
+ auto l1 = [] {};
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l1 = []() -> void {};
+ // CHECK-FIXES-CXX23: auto l1 = [] -> void {};
+
+ auto l2 = [] { return 1; };
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l2 = []() -> int { return 1; };
+ // CHECK-FIXES-CXX23: auto l2 = [] -> int { return 1; };
+
+ auto l3 = [&x] /* comment */ { return x; };
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l3 = [&x]() -> int /* comment */ { return x; };
+ // CHECK-FIXES-CXX23: auto l3 = [&x] /* comment */ -> int { return x; };
+
+ // Specifiers would have to follow '()', so don't offer a fix before C++23.
+ auto l4 = [x] mutable { return x; };
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l4 = [x] mutable { return x; };
+ // CHECK-FIXES-CXX23: auto l4 = [x] mutable -> int { return x; };
+
+#if __cplusplus < 202302L
+ auto l6 = []{ return 1; };
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l6 = []() -> int{ return 1; };
+
+ auto l7 = [] LAMBDA_BODY;
+ // CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-BEFORE-CXX23: auto l7 = [] LAMBDA_BODY;
+#endif
+
+#if __cplusplus >= 202002L
+ auto l5 = []<typename T = int> { return T{}; };
+ // CHECK-MESSAGES-CXX20: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX20: auto l5 = []<typename T = int>() -> auto { return T{}; };
+ // CHECK-FIXES-CXX23: auto l5 = []<typename T = int> -> auto { return T{}; };
+
+ // A requires clause would have to come before '()', so don't offer a fix
+ // before C++23.
+ auto l8 = []<typename T = int> requires true { return T{}; };
+ // CHECK-MESSAGES-CXX20: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX20: auto l8 = []<typename T = int> requires true { return T{}; };
+#endif
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas.cpp
index 7570ca3008aeb5..3036c4477d39ff 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas.cpp
@@ -14,10 +14,6 @@
#include <vector>
void test_lambda_positive() {
- auto l01 = [] {};
- // CHECK-MESSAGES-ALL: :[[@LINE-1]]:14: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
- // CHECK-MESSAGES-NOAUTO: :[[@LINE-2]]:14: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
- // CHECK-FIXES-ALL: auto l01 = [] -> void {};
auto l1 = []() {};
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-MESSAGES-NOAUTO: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
@@ -55,14 +51,14 @@ void test_lambda_positive() {
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-MESSAGES-NOAUTO: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-FIXES-ALL: auto l8 = [&capture_int]() -> int { return capture_int; };
- auto l9 = [] { return std::vector<std::vector<int>>{}; };
+ auto l9 = []() { return std::vector<std::vector<int>>{}; };
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-MESSAGES-NOAUTO: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
- // CHECK-FIXES-ALL: auto l9 = [] -> std::vector<std::vector<int>> { return std::vector<std::vector<int>>{}; };
- auto l10 = [] { const char* const * const * const ptr = nullptr; return ptr; };
+ // CHECK-FIXES-ALL: auto l9 = []() -> std::vector<std::vector<int>> { return std::vector<std::vector<int>>{}; };
+ auto l10 = []() { const char* const * const * const ptr = nullptr; return ptr; };
// CHECK-MESSAGES-ALL: :[[@LINE-1]]:14: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-MESSAGES-NOAUTO: :[[@LINE-2]]:14: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
- // CHECK-FIXES-ALL: auto l10 = [] -> const char *const *const * { const char* const * const * const ptr = nullptr; return ptr; };
+ // CHECK-FIXES-ALL: auto l10 = []() -> const char *const *const * { const char* const * const * const ptr = nullptr; return ptr; };
}
// In c++11 mode we can not write 'auto' type, see *-cxx14.cpp for fixes.
>From 57a7f91942c13fc316d73a51ad7bc3b9e770ff7a Mon Sep 17 00:00:00 2001
From: awrrrr <aryanroka360 at gmail.com>
Date: Sun, 8 Nov 2026 12:33:12 +0530
Subject: [PATCH 2/2] [clang-tidy] Fix use-trailing-return-type insert location
for lambdas
modernize-use-trailing-return-type inserted a lambda's trailing return type
one character before the body or `requires` clause. That split the preceding
token when nothing separated it from the body, so `[](){ return 1; }` became
`[]( -> int){ return 1; }`, and in C++23 `[]{ return 1; }` became
`[ -> int]{ return 1; }`. A comment before the body got the same treatment,
and the return type was lost after a line comment. When the body was the first
macro expansion in the file, as in `[] BODY`, the location before it wasn't
part of any expansion and clang-tidy crashed with "Not a macro expansion
SLocEntry!".
Insert the return type after the token before the body or `requires` clause
instead, skipping comments, and add a space if `requires` follows directly.
If the body comes from a macro, keep the warning but don't offer a fix, unless
the lambda is written inside a macro argument.
Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
.../modernize/UseTrailingReturnTypeCheck.cpp | 37 +++++-
clang-tools-extra/docs/ReleaseNotes.md | 5 +-
...ng-return-type-transform-lambdas-cxx20.cpp | 4 +-
...type-transform-lambdas-insert-location.cpp | 118 ++++++++++++++++++
...-transform-lambdas-no-parameter-clause.cpp | 2 +-
5 files changed, 157 insertions(+), 9 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-insert-location.cpp
diff --git a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
index 15246330b20e9e..33b75da965911e 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp
@@ -7,9 +7,11 @@
//===----------------------------------------------------------------------===//
#include "UseTrailingReturnTypeCheck.h"
+#include "../utils/LexerUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/CharInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/FixIt.h"
#include "llvm/ADT/StringExtras.h"
@@ -363,6 +365,26 @@ findReturnTypeAndCVSourceRange(const FunctionDecl &F, const TypeLoc &ReturnLoc,
return ReturnTypeRange;
}
+// Returns the end of the token before Loc, so that text inserted there can't
+// split a token or end up in a comment, or an invalid location if that token
+// can't be edited as part of the same lambda.
+static SourceLocation findEndOfPreviousToken(SourceLocation Loc,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ if (Loc.isMacroID()) {
+ // What comes before a location at the start of a macro argument isn't
+ // necessarily next to it in the file.
+ if (!SM.isMacroArgExpansion(Loc) ||
+ SM.isAtStartOfImmediateMacroExpansion(Loc))
+ return {};
+ Loc = SM.getSpellingLoc(Loc);
+ }
+
+ const std::optional<Token> PreviousToken =
+ utils::lexer::getPreviousToken(Loc, SM, LangOpts);
+ return PreviousToken ? PreviousToken->getEndLoc() : SourceLocation();
+}
+
static SourceLocation findLambdaTrailingReturnInsertLoc(
const CXXMethodDecl *Method, const SourceManager &SM,
const LangOptions &LangOpts, const ASTContext &Ctx) {
@@ -392,7 +414,7 @@ static SourceLocation findLambdaTrailingReturnInsertLoc(
}
if (Token.is(tok::kw_requires))
- return Token.getLocation().getLocWithOffset(-1);
+ return findEndOfPreviousToken(Token.getLocation(), SM, LangOpts);
}
return {};
@@ -400,7 +422,7 @@ static SourceLocation findLambdaTrailingReturnInsertLoc(
// If no requires clause, insert before the body
if (const Stmt *Body = Method->getBody())
- return Body->getBeginLoc().getLocWithOffset(-1);
+ return findEndOfPreviousToken(Body->getBeginLoc(), SM, LangOpts);
return {};
}
@@ -669,14 +691,19 @@ void UseTrailingReturnTypeCheck::diagOnLambda(
findLambdaTrailingReturnInsertLoc(Method, *Result.SourceManager,
getLangOpts(), *Result.Context);
- if (TrailingReturnInsertLoc.isValid())
+ if (TrailingReturnInsertLoc.isValid()) {
+ // Don't merge the return type with a directly following 'requires'.
+ const char *NextChar =
+ Result.SourceManager->getCharacterData(TrailingReturnInsertLoc);
diag(Lambda->getBeginLoc(), "use a trailing return type for this lambda")
<< FixItHint::CreateInsertion(
TrailingReturnInsertLoc,
" -> " +
- ReturnType.getAsString(Result.Context->getPrintingPolicy()));
- else
+ ReturnType.getAsString(Result.Context->getPrintingPolicy()) +
+ (isAsciiIdentifierContinue(*NextChar) ? " " : ""));
+ } else {
diag(Lambda->getBeginLoc(), ErrorMessageOnLambda);
+ }
}
} // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index c91ca2ea6c4bfd..fdcbad372118ae 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -332,7 +332,10 @@ infrastructure are described first, followed by tool-specific sections.
- Improved {doc}`modernize-use-trailing-return-type
<clang-tidy/checks/modernize/use-trailing-return-type>` check by adding `()`
to fixes for lambdas without a parameter clause before C++23, and not offering
- a fix when that would need specifiers to be moved.
+ a fix when that would need specifiers to be moved. The check also no longer
+ crashes on a lambda whose body comes from a macro, and no longer inserts the
+ return type inside the preceding token or comment, such as in
+ `[](){ return 1; }`.
- Improved {doc}`performance-inefficient-algorithm
<clang-tidy/checks/performance/inefficient-algorithm>` check to no longer
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp
index 24f135054fded7..7f773fa3954878 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-cxx20.cpp
@@ -25,7 +25,7 @@ void test_lambda_positive() {
requires std::floating_point<T> && std::floating_point<U>
{ return x * y; };
// CHECK-MESSAGES: :[[@LINE-3]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
- // CHECK-FIXES: auto l5 = []<typename T, typename U>(T x, U y) constexpr noexcept
- // CHECK-FIXES: -> auto requires std::floating_point<T> && std::floating_point<U>
+ // CHECK-FIXES: auto l5 = []<typename T, typename U>(T x, U y) constexpr noexcept -> auto
+ // CHECK-FIXES: requires std::floating_point<T> && std::floating_point<U>
// CHECK-FIXES: { return x * y; };
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-insert-location.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-insert-location.cpp
new file mode 100644
index 00000000000000..5e86ff8c4b6afa
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-insert-location.cpp
@@ -0,0 +1,118 @@
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s modernize-use-trailing-return-type %t
+// RUN: %check_clang_tidy -check-suffixes=,CXX20 -std=c++20 %s modernize-use-trailing-return-type %t
+// RUN: %check_clang_tidy -check-suffixes=,CXX20,CXX23 -std=c++23-or-later %s modernize-use-trailing-return-type %t
+
+// The return type has to go after the last token before the body, however that
+// token is separated from the body.
+
+#define ID(x) x
+#define APPLY_BODY(body) []() body
+#define PARENS ()
+#define BODY { return 1; }
+#define PARENS_AND_BODY () { return 1; }
+#define WHOLE_LAMBDA []() { return 1; }
+
+// This has to stay the first macro expansion in the file: the character before
+// the body then isn't part of any macro expansion, which used to crash.
+auto first_macro_expansion = [] BODY;
+// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+// CHECK-FIXES: auto first_macro_expansion = [] BODY;
+
+void test_body_in_macro() {
+ auto l1 = []() BODY;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l1 = []() BODY;
+
+ auto l2 = [] PARENS_AND_BODY;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l2 = [] PARENS_AND_BODY;
+
+ auto l3 = WHOLE_LAMBDA;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l3 = WHOLE_LAMBDA;
+
+ auto l4 = APPLY_BODY({ return 1; });
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l4 = APPLY_BODY({ return 1; });
+}
+
+void test_no_space_before_body() {
+ int x = 0;
+
+ auto l1 = [](){ return 1; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l1 = []() -> int{ return 1; };
+
+ auto l2 = [](int i){ return i; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l2 = [](int i) -> int{ return i; };
+
+ auto l3 = [x]() mutable{ return x; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l3 = [x]() mutable -> int{ return x; };
+
+ auto l4 = []() noexcept{ return 1; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l4 = []() noexcept -> int{ return 1; };
+
+ auto l5 = [] PARENS{ return 1; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l5 = [] PARENS -> int{ return 1; };
+
+#if __cplusplus >= 202302L
+ auto l6 = []{ return 1; };
+ // CHECK-MESSAGES-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX23: auto l6 = [] -> int{ return 1; };
+#endif
+}
+
+void test_comment_before_body() {
+ auto l1 = []() /* comment */{ return 1; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l1 = []() -> int /* comment */{ return 1; };
+
+ auto l2 = []() /* comment */ { return 1; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l2 = []() -> int /* comment */ { return 1; };
+
+ auto l3 = []() // comment
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l3 = []() -> int // comment
+ { return 1; };
+}
+
+void test_lambda_in_macro_argument() {
+ auto l1 = ID([]() { return 1; });
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l1 = ID([]() -> int { return 1; });
+
+ auto l2 = ID([](){ return 1; });
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l2 = ID([]() -> int{ return 1; });
+
+ auto l3 = ID(ID([](){ return 1; }));
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES: auto l3 = ID(ID([]() -> int{ return 1; }));
+}
+
+#if __cplusplus >= 202002L
+template <typename T>
+void test_requires_clause(T t) {
+ auto l1 = [](int i)requires true { return i; };
+ // CHECK-MESSAGES-CXX20: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX20: auto l1 = [](int i) -> auto requires true { return i; };
+
+ auto l2 = [](int i) /* comment */requires true { return i; };
+ // CHECK-MESSAGES-CXX20: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX20: auto l2 = [](int i) -> auto /* comment */requires true { return i; };
+
+ auto l3 = [](int i) // comment
+ // CHECK-MESSAGES-CXX20: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+ // CHECK-FIXES-CXX20: auto l3 = [](int i) -> auto // comment
+ requires true { return i; };
+}
+
+auto generic_no_space = []<typename T>(T t){ return 1; };
+// CHECK-MESSAGES-CXX20: :[[@LINE-1]]:25: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
+// CHECK-FIXES-CXX20: auto generic_no_space = []<typename T>(T t) -> auto{ return 1; };
+#endif
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
index 3a78c6581bd1a4..914992dbcb1ffd 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-trailing-return-type-transform-lambdas-no-parameter-clause.cpp
@@ -26,7 +26,7 @@ void test_no_parameter_clause() {
// CHECK-MESSAGES-BEFORE-CXX23: :[[@LINE-1]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-MESSAGES-CXX23: :[[@LINE-2]]:13: warning: use a trailing return type for this lambda [modernize-use-trailing-return-type]
// CHECK-FIXES-BEFORE-CXX23: auto l3 = [&x]() -> int /* comment */ { return x; };
- // CHECK-FIXES-CXX23: auto l3 = [&x] /* comment */ -> int { return x; };
+ // CHECK-FIXES-CXX23: auto l3 = [&x] -> int /* comment */ { return x; };
// Specifiers would have to follow '()', so don't offer a fix before C++23.
auto l4 = [x] mutable { return x; };
More information about the cfe-commits
mailing list