[clang-tools-extra] [clang-tidy] Add support for lambda-expression in `use-trailing-return-type` check (PR #135383)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 11 07:55:50 PDT 2025
================
@@ -494,4 +554,75 @@ void UseTrailingReturnTypeCheck::check(const MatchFinder::MatchResult &Result) {
<< FixItHint::CreateInsertion(InsertionLoc, " -> " + ReturnType);
}
+void UseTrailingReturnTypeCheck::diagOnLambda(
+ const LambdaExpr *Lambda,
+ const ast_matchers::MatchFinder::MatchResult &Result) {
+
+ const CXXMethodDecl *Method = Lambda->getCallOperator();
+ if (!Method || Lambda->hasExplicitResultType())
+ return;
+
+ const QualType ReturnType = Method->getReturnType();
+ if (ReturnType->isUndeducedAutoType() &&
+ TransformLambdas == TransformLambda::AllExceptAuto)
+ return;
+
+ const SourceLocation TrailingReturnInsertLoc =
+ findLambdaTrailingReturnInsertLoc(Method, *Result.SourceManager,
+ getLangOpts(), *Result.Context);
+
+ if (TrailingReturnInsertLoc.isValid())
+ diag(Lambda->getBeginLoc(), "use a trailing return type for this lambda")
+ << FixItHint::CreateInsertion(
+ TrailingReturnInsertLoc,
+ " -> " +
+ ReturnType.getAsString(Result.Context->getPrintingPolicy()));
+ else
+ diag(Lambda->getBeginLoc(), "use a trailing return type for this lambda");
+}
+
+SourceLocation UseTrailingReturnTypeCheck::findLambdaTrailingReturnInsertLoc(
+ const CXXMethodDecl *Method, const SourceManager &SM,
+ const LangOptions &LangOpts, const ASTContext &Ctx) {
+ // 'requires' keyword is present in lambda declaration
+ if (Method->getTrailingRequiresClause()) {
+ SourceLocation ParamEndLoc;
+ if (Method->param_empty()) {
+ ParamEndLoc = Method->getBeginLoc();
+ } else {
+ ParamEndLoc = Method->getParametersSourceRange().getEnd();
+ }
+
+ std::pair<FileID, unsigned> ParamEndLocInfo =
----------------
vbvictor wrote:
This code is very alike to `findTrailingReturnTypeSourceLocation` function. Finding the position of `requires` keyword.
https://github.com/llvm/llvm-project/pull/135383
More information about the cfe-commits
mailing list