Skip to content

Commit edac2d4

Browse files
committed
[NFC] Move init expression logic to registerMatchers
1 parent 5bdf206 commit edac2d4

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static constexpr llvm::StringLiteral ScopeBlockName = "ScopeBlock";
2828
static constexpr llvm::StringLiteral StdTieAssignStmtName = "StdTieAssign";
2929
static constexpr llvm::StringLiteral StdTieExprName = "StdTieExpr";
3030
static constexpr llvm::StringLiteral ForRangeStmtName = "ForRangeStmt";
31+
static constexpr llvm::StringLiteral InitExprName = "init_expr";
3132

3233
/// Matches a sequence of VarDecls matching the inner matchers, starting from
3334
/// the \p Iter to \p EndIter and set bindings for the first DeclStmt and the
@@ -273,7 +274,7 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
273274
hasType(qualType(anyOf(PairType, lValueReferenceType(
274275
pointee(PairType))))
275276
.bind(PairVarTypeName)),
276-
hasInitializer(expr()))
277+
hasInitializer(expr(ignoringCopyCtorAndImplicitCast(expr().bind(InitExprName)))))
277278
.bind(PairDeclName)),
278279
hasNextTwoVarDecl(
279280
llvm::SmallVector<ast_matchers::internal::Matcher<VarDecl>>{
@@ -293,7 +294,7 @@ void UseStructuredBindingCheck::registerMatchers(MatchFinder *Finder) {
293294
varDecl(hasType(qualType(anyOf(PairType, lValueReferenceType(
294295
pointee(PairType))))
295296
.bind(PairVarTypeName)),
296-
hasInitializer(expr()))
297+
hasInitializer(expr(ignoringCopyCtorAndImplicitCast(expr().bind(InitExprName)))))
297298
.bind(PairDeclName)),
298299
hasBody(
299300
compoundStmt(
@@ -375,12 +376,7 @@ void UseStructuredBindingCheck::check(const MatchFinder::MatchResult &Result) {
375376
// Check whether PairVar, FirstVar and SecondVar have the same transfer type,
376377
// so they can be combined to structured binding.
377378
const auto *PairVar = Result.Nodes.getNodeAs<VarDecl>(PairDeclName);
378-
const Expr *InitE = PairVar->getInit();
379-
if (auto Res =
380-
match(expr(ignoringCopyCtorAndImplicitCast(expr().bind("init_expr"))),
381-
*InitE, *Result.Context);
382-
!Res.empty())
383-
InitE = Res[0].getNodeAs<Expr>("init_expr");
379+
const Expr *InitE = Result.Nodes.getNodeAs<Expr>(InitExprName);
384380

385381
const std::optional<TransferType> PairCaptureType =
386382
getTransferType(*Result.Context, PairVar->getType(), InitE->getType());

0 commit comments

Comments
 (0)