Skip to content

Commit 2ebab57

Browse files
committed
[NFC] Place lambda match operation to registerMatchers
1 parent c6f6885 commit 2ebab57

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

clang-tools-extra/clang-tidy/modernize/UseStructuredBindingCheck.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
223223
return declRefExpr(to(varDecl(UnlessShouldBeIgnored).bind(Name)));
224224
};
225225

226+
auto HasAnyLambdaCaptureThisVar =
227+
[](ast_matchers::internal::Matcher<VarDecl> VDMatcher)
228+
-> ast_matchers::internal::BindableMatcher<Stmt> {
229+
return compoundStmt(hasDescendant(
230+
lambdaExpr(hasAnyCapture(capturesVar(varDecl(VDMatcher))))));
231+
};
232+
233+
// Captured structured bindings are a C++20 extension
234+
auto UnlessFirstVarOrSecondVarIsCapturedByLambda =
235+
getLangOpts().CPlusPlus20
236+
? compoundStmt()
237+
: compoundStmt(unless(HasAnyLambdaCaptureThisVar(
238+
anyOf(equalsBoundNode(std::string(FirstVarDeclName)),
239+
equalsBoundNode(std::string(SecondVarDeclName))))));
240+
226241
// X x;
227242
// Y y;
228243
// std::tie(x, y) = ...;
@@ -243,7 +258,8 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
243258
llvm::SmallVector<ast_matchers::internal::Matcher<VarDecl>>{
244259
varDecl(equalsBoundNode(std::string(FirstVarDeclName))),
245260
varDecl(equalsBoundNode(std::string(SecondVarDeclName)))}),
246-
hasParent(compoundStmt().bind(ScopeBlockName))),
261+
hasParent(compoundStmt(UnlessFirstVarOrSecondVarIsCapturedByLambda)
262+
.bind(ScopeBlockName))),
247263
this);
248264

249265
// pair<X, Y> p = ...;
@@ -262,7 +278,8 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
262278
hasNextTwoVarDecl(
263279
llvm::SmallVector<ast_matchers::internal::Matcher<VarDecl>>{
264280
VarInitWithFirstMember, VarInitWithSecondMember}),
265-
hasParent(compoundStmt().bind(ScopeBlockName))),
281+
hasParent(compoundStmt(UnlessFirstVarOrSecondVarIsCapturedByLambda)
282+
.bind(ScopeBlockName))),
266283
this);
267284

268285
// for (pair<X, Y> p : map) {
@@ -282,7 +299,8 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
282299
compoundStmt(
283300
hasFirstTwoVarDecl(llvm::SmallVector<
284301
ast_matchers::internal::Matcher<VarDecl>>{
285-
VarInitWithFirstMember, VarInitWithSecondMember}))
302+
VarInitWithFirstMember, VarInitWithSecondMember}),
303+
UnlessFirstVarOrSecondVarIsCapturedByLambda)
286304
.bind(ScopeBlockName)))
287305
.bind(ForRangeStmtName),
288306
this);
@@ -317,17 +335,6 @@ void UseStructuredBindingCheck::check(const MatchFinder::MatchResult &Result) {
317335
const auto *EndDS = Result.Nodes.getNodeAs<DeclStmt>(EndDeclStmtName);
318336
const auto *ScopeBlock = Result.Nodes.getNodeAs<CompoundStmt>(ScopeBlockName);
319337

320-
// Captured structured bindings are a C++20 extension
321-
if (!Result.Context->getLangOpts().CPlusPlus20) {
322-
if (auto Matchers = match(
323-
compoundStmt(
324-
hasDescendant(lambdaExpr(hasAnyCapture(capturesVar(varDecl(
325-
anyOf(equalsNode(FirstVar), equalsNode(SecondVar)))))))),
326-
*ScopeBlock, *Result.Context);
327-
!Matchers.empty())
328-
return;
329-
}
330-
331338
const auto *CFRS = Result.Nodes.getNodeAs<CXXForRangeStmt>(ForRangeStmtName);
332339
auto DiagAndFix = [&BeginDS, &EndDS, &FirstVar, &SecondVar, &CFRS,
333340
this](SourceLocation DiagLoc, SourceRange ReplaceRange,

0 commit comments

Comments
 (0)