32 ASTContext &Context) {
33 for (
const LambdaCapture &Capture : Lambda->explicit_captures()) {
34 if (Capture.isExplicit()) {
35 if (Capture.getCaptureKind() == LCK_ByRef) {
36 const SourceManager &SourceMgr = Context.getSourceManager();
38 Capture.getLocation(), SourceMgr, Context.getLangOpts(), tok::amp);
41 return Capture.getLocation();
44 return Lambda->getIntroducerRange().getEnd();
48 std::string Replacement;
49 llvm::raw_string_ostream Stream(Replacement);
51 auto AppendName = [&](llvm::StringRef Name) {
52 if (!Replacement.empty())
54 if (Lambda->getCaptureDefault() == LCD_ByRef && Name !=
"this")
55 Stream <<
"&" << Name;
60 for (
const LambdaCapture &Capture : Lambda->implicit_captures()) {
61 assert(Capture.isImplicit());
62 if (Capture.capturesVariable() && Capture.isImplicit())
63 AppendName(Capture.getCapturedVar()->getName());
64 else if (Capture.capturesThis())
67 if (!Replacement.empty() && !Lambda->explicit_captures().empty()) {
75 const MatchFinder::MatchResult &Result) {
76 const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>(
"lambda");
80 if (Lambda->getCaptureDefault() == LCD_ByCopy) {
81 const bool IsThisImplicitlyCaptured = std::any_of(
82 Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
83 [](
const LambdaCapture &Capture) { return Capture.capturesThis(); });
84 auto Diag = diag(Lambda->getCaptureDefaultLoc(),
85 "lambdas that %select{|implicitly }0capture 'this' "
86 "should not specify a by-value capture default")
87 << IsThisImplicitlyCaptured;
90 const SourceLocation DefaultCaptureEnd =
92 Diag << FixItHint::CreateReplacement(
93 CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.