[clang-tools-extra] [clang-tidy] Add new check `modernize-use-structured-binding` (PR #158462)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 19 00:56:27 PDT 2025
================
@@ -0,0 +1,419 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseStructuredBindingCheck.h"
+#include "../utils/DeclRefExprUtils.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+namespace {
+constexpr const char *DefaultPairTypes = "std::pair";
+constexpr llvm::StringLiteral PairDeclName = "PairVarD";
+constexpr llvm::StringLiteral PairVarTypeName = "PairVarType";
+constexpr llvm::StringLiteral FirstVarDeclName = "FirstVarDecl";
+constexpr llvm::StringLiteral SecondVarDeclName = "SecondVarDecl";
+constexpr llvm::StringLiteral FirstDeclStmtName = "FirstDeclStmt";
+constexpr llvm::StringLiteral SecondDeclStmtName = "SecondDeclStmt";
+constexpr llvm::StringLiteral FirstTypeName = "FirstType";
+constexpr llvm::StringLiteral SecondTypeName = "SecondType";
+constexpr llvm::StringLiteral ScopeBlockName = "ScopeBlock";
+constexpr llvm::StringLiteral StdTieAssignStmtName = "StdTieAssign";
+constexpr llvm::StringLiteral StdTieExprName = "StdTieExpr";
+constexpr llvm::StringLiteral ForRangeStmtName = "ForRangeStmt";
+
+/// What qualifiers and specifiers are used to create structured binding
+/// declaration, it only supports the following four cases now.
+enum TransferType : uint8_t {
+ TT_ByVal,
+ TT_ByConstVal,
+ TT_ByRef,
+ TT_ByConstRef
+};
+
+/// Try to match exactly two VarDecl inside two DeclStmts, and set binding for
+/// the used DeclStmts.
+bool matchTwoVarDecl(const DeclStmt *DS1, const DeclStmt *DS2,
+ ast_matchers::internal::Matcher<VarDecl> InnerMatcher1,
+ ast_matchers::internal::Matcher<VarDecl> InnerMatcher2,
+ internal::ASTMatchFinder *Finder,
+ internal::BoundNodesTreeBuilder *Builder) {
+ SmallVector<std::pair<const VarDecl *, const DeclStmt *>, 2> Vars;
+ auto CollectVarsInDeclStmt = [&Vars](const DeclStmt *DS) -> bool {
+ if (!DS)
+ return true;
+
+ for (const auto *VD : DS->decls()) {
+ if (Vars.size() == 2)
+ return false;
+
+ if (const auto *Var = dyn_cast<VarDecl>(VD))
+ Vars.emplace_back(Var, DS);
+ else
+ return false;
+ }
----------------
vbvictor wrote:
I agree how complicated these matchers are and I couldn't commit myself to review them this week.
I wonder if we could have a matcher on `CompoundStmt` that will match if N sub-statements go one after another in sequence. I faced this problem in `use-scoped-lock` check and decided to move "in sequence" logic outside of matchers. Personally I don't mind moving some parts outside matchers if it gives more readable and maintainable code despite being slower
https://github.com/llvm/llvm-project/pull/158462
More information about the cfe-commits
mailing list