================ @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "AvoidDefaultLambdaCaptureCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/Lambda.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::readability { + +static std::string generateCaptureText(const LambdaCapture &Capture) { + if (Capture.capturesThis()) + return Capture.getCaptureKind() == LCK_StarThis ? "*this" : "this"; + + std::string Result; + if (Capture.getCaptureKind() == LCK_ByRef) + Result += "&"; + + Result += Capture.getCapturedVar()->getName().str(); + return Result; +} + +AvoidDefaultLambdaCaptureCheck::AvoidDefaultLambdaCaptureCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + IgnoreImplicitCapturesInSTL( + Options.get("IgnoreImplicitCapturesInSTL", false)) {} + +void AvoidDefaultLambdaCaptureCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreImplicitCapturesInSTL", + IgnoreImplicitCapturesInSTL); +} + +void AvoidDefaultLambdaCaptureCheck::registerMatchers(MatchFinder *Finder) { + if (IgnoreImplicitCapturesInSTL) { + Finder->addMatcher( + lambdaExpr(hasDefaultCapture(), + unless(hasAncestor( + callExpr(callee(functionDecl(isInStdNamespace())))))) ---------------- jjmarr-amd wrote:
No? This seems to be right. My understanding is a default capture could cause an unintentional dangling reference if the lambda is stored. However, `std` functions in `<algorithm>` and `<numeric>` don't need to worry about dangling since the lambda is immediately executed. Since this nested lambda construction is still immediately passed to `std::find`, it won't dangle. It's not possible for `x` to dangle in this. https://github.com/llvm/llvm-project/pull/160150 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
