[clang-tools-extra] [clang-tidy][readability-Identifier-length] add structured bindings support (PR #193838)
Alex Dutka via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 23 14:03:33 PDT 2026
https://github.com/dutkalex created https://github.com/llvm/llvm-project/pull/193838
This PR extends the `readability-identifier-length` to structured binding declarations. Currently, only the names of regular variables, function parameters, exceptions variables in catch blocks and loop variables are checked. In other words, `int a = ...` warns but `auto [a] = ...` does not.
This PR enables the check to warn when the length of the names introduced in structured bindings declarations is too short, following the same rules as for the other kinds of declarations. Two new options are introduced:
- `MinimumBindingNameLength` defines the number of characters below which a warning is issued (the default it set to 2, like for exceptions variables)
- `IgnoredBindingNames` defines the names for which warnings should not issued (by default, `_` is the only allowed exception)
To implement this feature, a new matcher is registered which matches all the individual binding names. For example, in `auto [x, y] = ...`, it matches `x` and `y` separately and the logic for determining if a warning should be issued is applied independently to each binding name introduced. Unlike the other matchers which return `VarDecl` nodes, this new matcher returns `BindingDecl` nodes. The `utils::allDeclRefExpr` helper function is modified to work for any `ValueDecl` node (the closest common ancestor of `VarDecl` and `BindingDecl`) in order for the line count threshold feature to work with this extension.
>From d14d1e3446fa72db45ee8f263e22117259e7b263 Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Wed, 22 Apr 2026 21:51:33 +0200
Subject: [PATCH 1/8] add matcher for structured bindings
---
.../readability/IdentifierLengthCheck.cpp | 27 ++++++++++++++++++-
.../readability/IdentifierLengthCheck.h | 4 +++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
index 82861065206ce..81a954a1cccc9 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
@@ -15,11 +15,13 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {
const unsigned DefaultMinimumVariableNameLength = 3;
+const unsigned DefaultMinimumBindingNameLength = 2;
const unsigned DefaultMinimumLoopCounterNameLength = 2;
const unsigned DefaultMinimumExceptionNameLength = 2;
const unsigned DefaultMinimumParameterNameLength = 3;
-const char DefaultIgnoredLoopCounterNames[] = "^[ijk_]$";
const char DefaultIgnoredVariableNames[] = "";
+const char DefaultIgnoredBindingNames[] = "_";
+const char DefaultIgnoredLoopCounterNames[] = "^[ijk_]$";
const char DefaultIgnoredExceptionVariableNames[] = "^[e]$";
const char DefaultIgnoredParameterNames[] = "^[n]$";
const unsigned DefaultLineCountThreshold = 0;
@@ -33,6 +35,8 @@ IdentifierLengthCheck::IdentifierLengthCheck(StringRef Name,
: ClangTidyCheck(Name, Context),
MinimumVariableNameLength(Options.get("MinimumVariableNameLength",
DefaultMinimumVariableNameLength)),
+ MinimumBindingNameLength(Options.get("MinimumBindingNameLength",
+ DefaultMinimumBindingNameLength)),
MinimumLoopCounterNameLength(Options.get(
"MinimumLoopCounterNameLength", DefaultMinimumLoopCounterNameLength)),
MinimumExceptionNameLength(Options.get(
@@ -42,6 +46,9 @@ IdentifierLengthCheck::IdentifierLengthCheck(StringRef Name,
IgnoredVariableNamesInput(
Options.get("IgnoredVariableNames", DefaultIgnoredVariableNames)),
IgnoredVariableNames(IgnoredVariableNamesInput),
+ IgnoredBindingNamesInput(
+ Options.get("IgnoredBindingNames", DefaultIgnoredBindingNames)),
+ IgnoredBindingNames(IgnoredBindingNamesInput),
IgnoredLoopCounterNamesInput(Options.get("IgnoredLoopCounterNames",
DefaultIgnoredLoopCounterNames)),
IgnoredLoopCounterNames(IgnoredLoopCounterNamesInput),
@@ -82,6 +89,9 @@ void IdentifierLengthCheck::registerMatchers(MatchFinder *Finder) {
if (MinimumParameterNameLength > 1)
Finder->addMatcher(parmVarDecl().bind("paramVar"), this);
+ if (MinimumBindingNameLength > 1)
+ Finder->addMatcher(bindingDecl().bind("bindingVar"), this);
+
if (MinimumVariableNameLength > 1)
Finder->addMatcher(
varDecl(unless(anyOf(hasParent(declStmt(hasParent(forStmt()))),
@@ -145,6 +155,21 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
<< 0 << StandaloneVar << MinimumVariableNameLength;
}
+ const auto *BindingVar = Result.Nodes.getNodeAs<VarDecl>("bindingVar");
+ if (BindingVar) {
+ if (!BindingVar->getIdentifier())
+ return;
+
+ const StringRef VarName = BindingVar->getName();
+
+ if (VarName.size() >= MinimumBindingNameLength ||
+ IgnoredBindingNames.match(VarName))
+ return;
+
+ diag(BindingVar->getLocation(), ErrorMessage)
+ << 0 << BindingVar << MinimumBindingNameLength;
+ }
+
auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>("exceptionVar");
if (ExceptionVarName) {
if (!ExceptionVarName->getIdentifier())
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.h
index 2a59788260f8c..8fd762971b01b 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.h
@@ -27,6 +27,7 @@ class IdentifierLengthCheck : public ClangTidyCheck {
private:
const unsigned MinimumVariableNameLength;
+ const unsigned MinimumBindingNameLength;
const unsigned MinimumLoopCounterNameLength;
const unsigned MinimumExceptionNameLength;
const unsigned MinimumParameterNameLength;
@@ -34,6 +35,9 @@ class IdentifierLengthCheck : public ClangTidyCheck {
std::string IgnoredVariableNamesInput;
llvm::Regex IgnoredVariableNames;
+ std::string IgnoredBindingNamesInput;
+ llvm::Regex IgnoredBindingNames;
+
std::string IgnoredLoopCounterNamesInput;
llvm::Regex IgnoredLoopCounterNames;
>From 2a5ae5b9276736c649f811283e0567c2d198ed94 Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 00:59:36 +0200
Subject: [PATCH 2/8] add missing updates
---
.../clang-tidy/readability/IdentifierLengthCheck.cpp | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
index 81a954a1cccc9..3710e02104ef4 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
@@ -64,12 +64,14 @@ IdentifierLengthCheck::IdentifierLengthCheck(StringRef Name,
void IdentifierLengthCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "MinimumVariableNameLength", MinimumVariableNameLength);
+ Options.store(Opts, "MinimumBindingNameLength", MinimumBindingNameLength);
Options.store(Opts, "MinimumLoopCounterNameLength",
MinimumLoopCounterNameLength);
Options.store(Opts, "MinimumExceptionNameLength", MinimumExceptionNameLength);
Options.store(Opts, "MinimumParameterNameLength", MinimumParameterNameLength);
- Options.store(Opts, "IgnoredLoopCounterNames", IgnoredLoopCounterNamesInput);
Options.store(Opts, "IgnoredVariableNames", IgnoredVariableNamesInput);
+ Options.store(Opts, "IgnoredBindingNames", IgnoredBindingNamesInput);
+ Options.store(Opts, "IgnoredLoopCounterNames", IgnoredLoopCounterNamesInput);
Options.store(Opts, "IgnoredExceptionVariableNames",
IgnoredExceptionVariableNamesInput);
Options.store(Opts, "IgnoredParameterNames", IgnoredParameterNamesInput);
@@ -166,6 +168,10 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
IgnoredBindingNames.match(VarName))
return;
+ if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
+ LineCountThreshold))
+ return;
+
diag(BindingVar->getLocation(), ErrorMessage)
<< 0 << BindingVar << MinimumBindingNameLength;
}
>From bb6dfe95dc0a8225f148b978f6a07a584a56dd0f Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 02:02:24 +0200
Subject: [PATCH 3/8] add new test file for structured bindings
---
.../identifier-length-structured-bindings.cpp | 23 +++++++++++++++++++
1 file changed, 23 insertions(+)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
new file mode 100644
index 0000000000000..2044c5b28b908
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
@@ -0,0 +1,23 @@
+
+// RUN: %check_clang_tidy -std=c++17-or-later %s readability-identifier-length %t \
+// RUN: -config='{CheckOptions: {}}' \
+// RUN: -- -fexceptions
+
+struct aggregate {
+ int first;
+ double middle;
+ bool last;
+};
+
+aggregate get_data();
+
+void shouldWarn() {
+ auto [f, m, l] = get_data();
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: binding variable name 'f' is too short, expected at least 2 characters [readability-identifier-length]
+ // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: binding variable name 'm' is too short, expected at least 2 characters [readability-identifier-length]
+ // CHECK-MESSAGES: :[[@LINE-3]]:15: warning: binding variable name 'l' is too short, expected at least 2 characters [readability-identifier-length]
+}
+
+void shouldNotWarn() {
+ auto [_, mid, last] = get_data();
+}
>From 1bbe3ecba4f956b9f381c457af42fd29c4cb216a Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 02:03:09 +0200
Subject: [PATCH 4/8] extend error message
---
.../readability/IdentifierLengthCheck.cpp | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
index 3710e02104ef4..6ddac5a738ab6 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
@@ -27,7 +27,7 @@ const char DefaultIgnoredParameterNames[] = "^[n]$";
const unsigned DefaultLineCountThreshold = 0;
const char ErrorMessage[] =
- "%select{variable|exception variable|loop variable|"
+ "%select{variable|binding variable|exception variable|loop variable|"
"parameter}0 name %1 is too short, expected at least %2 characters";
IdentifierLengthCheck::IdentifierLengthCheck(StringRef Name,
@@ -157,7 +157,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
<< 0 << StandaloneVar << MinimumVariableNameLength;
}
- const auto *BindingVar = Result.Nodes.getNodeAs<VarDecl>("bindingVar");
+ const auto *BindingVar = Result.Nodes.getNodeAs<BindingDecl>("bindingVar");
if (BindingVar) {
if (!BindingVar->getIdentifier())
return;
@@ -168,12 +168,12 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
IgnoredBindingNames.match(VarName))
return;
- if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
- LineCountThreshold))
- return;
+ // if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
+ // LineCountThreshold))
+ // return;
diag(BindingVar->getLocation(), ErrorMessage)
- << 0 << BindingVar << MinimumBindingNameLength;
+ << 1 << BindingVar << MinimumBindingNameLength;
}
auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>("exceptionVar");
@@ -191,7 +191,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
return;
diag(ExceptionVarName->getLocation(), ErrorMessage)
- << 1 << ExceptionVarName << MinimumExceptionNameLength;
+ << 2 << ExceptionVarName << MinimumExceptionNameLength;
}
const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>("loopVar");
@@ -210,7 +210,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
return;
diag(LoopVar->getLocation(), ErrorMessage)
- << 2 << LoopVar << MinimumLoopCounterNameLength;
+ << 3 << LoopVar << MinimumLoopCounterNameLength;
}
const auto *ParamVar = Result.Nodes.getNodeAs<VarDecl>("paramVar");
@@ -229,7 +229,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
return;
diag(ParamVar->getLocation(), ErrorMessage)
- << 3 << ParamVar << MinimumParameterNameLength;
+ << 4 << ParamVar << MinimumParameterNameLength;
}
}
>From b63cae4c9a8c4961cfb2d511a0d107d914b6000b Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 02:28:32 +0200
Subject: [PATCH 5/8] change allDeclRefExpr input to ValueDecl
---
clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp | 4 ++--
clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.h | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index fbcdd1004cec0..c1c2db31e9c64 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -367,7 +367,7 @@ bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
}
SmallPtrSet<const DeclRefExpr *, 16>
-allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
+allDeclRefExprs(const ValueDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
auto Matches = match(
findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
Stmt, Context);
@@ -377,7 +377,7 @@ allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
}
SmallPtrSet<const DeclRefExpr *, 16>
-allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
+allDeclRefExprs(const ValueDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
auto Matches = match(
decl(forEachDescendant(
declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.h b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.h
index 794adc04dc478..5aa8f69fcff72 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.h
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.h
@@ -17,11 +17,11 @@ namespace clang::tidy::utils::decl_ref_expr {
/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Stmt``.
llvm::SmallPtrSet<const DeclRefExpr *, 16>
-allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context);
+allDeclRefExprs(const ValueDecl &VarDecl, const Stmt &Stmt, ASTContext &Context);
/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Decl``.
llvm::SmallPtrSet<const DeclRefExpr *, 16>
-allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context);
+allDeclRefExprs(const ValueDecl &VarDecl, const Decl &Decl, ASTContext &Context);
/// Returns set of all ``DeclRefExprs`` to ``VarDecl`` within ``Stmt`` where
/// ``VarDecl`` is guaranteed to be accessed in a const fashion.
>From 4a27c9a845a8ac58d4595700d4a41c34117029e3 Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 02:29:23 +0200
Subject: [PATCH 6/8] use ValueDecl instead of VarDecl and BindingDecl
---
.../readability/IdentifierLengthCheck.cpp | 22 +++++++++----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
index 6ddac5a738ab6..cb7284e1ae9f8 100644
--- a/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/IdentifierLengthCheck.cpp
@@ -20,7 +20,7 @@ const unsigned DefaultMinimumLoopCounterNameLength = 2;
const unsigned DefaultMinimumExceptionNameLength = 2;
const unsigned DefaultMinimumParameterNameLength = 3;
const char DefaultIgnoredVariableNames[] = "";
-const char DefaultIgnoredBindingNames[] = "_";
+const char DefaultIgnoredBindingNames[] = "^[_]$";
const char DefaultIgnoredLoopCounterNames[] = "^[ijk_]$";
const char DefaultIgnoredExceptionVariableNames[] = "^[e]$";
const char DefaultIgnoredParameterNames[] = "^[n]$";
@@ -102,7 +102,7 @@ void IdentifierLengthCheck::registerMatchers(MatchFinder *Finder) {
this);
}
-static std::optional<unsigned> countLinesToLastUse(const VarDecl *Var,
+static std::optional<unsigned> countLinesToLastUse(const ValueDecl *Var,
const SourceManager *SrcMgr,
ASTContext *Ctx) {
const auto *ParentScope = llvm::dyn_cast<FunctionDecl>(Var->getDeclContext());
@@ -125,7 +125,7 @@ static std::optional<unsigned> countLinesToLastUse(const VarDecl *Var,
return LastUseLine - DeclLine + 1;
}
-static bool isShortLived(const VarDecl *Var, const SourceManager *SrcMgr,
+static bool isShortLived(const ValueDecl *Var, const SourceManager *SrcMgr,
ASTContext *Ctx, unsigned LineCountThreshold) {
if (LineCountThreshold == 0)
return false;
@@ -138,7 +138,7 @@ static bool isShortLived(const VarDecl *Var, const SourceManager *SrcMgr,
}
void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *StandaloneVar = Result.Nodes.getNodeAs<VarDecl>("standaloneVar");
+ const auto *StandaloneVar = Result.Nodes.getNodeAs<ValueDecl>("standaloneVar");
if (StandaloneVar) {
if (!StandaloneVar->getIdentifier())
return;
@@ -157,7 +157,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
<< 0 << StandaloneVar << MinimumVariableNameLength;
}
- const auto *BindingVar = Result.Nodes.getNodeAs<BindingDecl>("bindingVar");
+ const auto *BindingVar = Result.Nodes.getNodeAs<ValueDecl>("bindingVar");
if (BindingVar) {
if (!BindingVar->getIdentifier())
return;
@@ -168,15 +168,15 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
IgnoredBindingNames.match(VarName))
return;
- // if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
- // LineCountThreshold))
- // return;
+ if (isShortLived(BindingVar, Result.SourceManager, Result.Context,
+ LineCountThreshold))
+ return;
diag(BindingVar->getLocation(), ErrorMessage)
<< 1 << BindingVar << MinimumBindingNameLength;
}
- auto *ExceptionVarName = Result.Nodes.getNodeAs<VarDecl>("exceptionVar");
+ auto *ExceptionVarName = Result.Nodes.getNodeAs<ValueDecl>("exceptionVar");
if (ExceptionVarName) {
if (!ExceptionVarName->getIdentifier())
return;
@@ -194,7 +194,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
<< 2 << ExceptionVarName << MinimumExceptionNameLength;
}
- const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>("loopVar");
+ const auto *LoopVar = Result.Nodes.getNodeAs<ValueDecl>("loopVar");
if (LoopVar) {
if (!LoopVar->getIdentifier())
return;
@@ -213,7 +213,7 @@ void IdentifierLengthCheck::check(const MatchFinder::MatchResult &Result) {
<< 3 << LoopVar << MinimumLoopCounterNameLength;
}
- const auto *ParamVar = Result.Nodes.getNodeAs<VarDecl>("paramVar");
+ const auto *ParamVar = Result.Nodes.getNodeAs<ValueDecl>("paramVar");
if (ParamVar) {
if (!ParamVar->getIdentifier())
return;
>From 186e068d21e7b13e9141c6bf142f0aba3f690c65 Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 02:40:51 +0200
Subject: [PATCH 7/8] improve test
---
.../identifier-length-structured-bindings.cpp | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
index 2044c5b28b908..61db34c5c875d 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-length-structured-bindings.cpp
@@ -1,6 +1,6 @@
// RUN: %check_clang_tidy -std=c++17-or-later %s readability-identifier-length %t \
-// RUN: -config='{CheckOptions: {}}' \
+// RUN: -config='{CheckOptions: {readability-identifier-length.LineCountThreshold: 1}}' \
// RUN: -- -fexceptions
struct aggregate {
@@ -11,13 +11,18 @@ struct aggregate {
aggregate get_data();
+template<typename... T>
+void doIt(T...);
+
void shouldWarn() {
auto [f, m, l] = get_data();
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: binding variable name 'f' is too short, expected at least 2 characters [readability-identifier-length]
// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: binding variable name 'm' is too short, expected at least 2 characters [readability-identifier-length]
// CHECK-MESSAGES: :[[@LINE-3]]:15: warning: binding variable name 'l' is too short, expected at least 2 characters [readability-identifier-length]
+ doIt(f, m, l);
}
void shouldNotWarn() {
- auto [_, mid, last] = get_data();
+ auto [_, mid, last] = get_data(); // '_' is accepted by default
+ doIt(_, mid, last);
}
>From 605f4c9d664b572d400d7cb021614cda76d33367 Mon Sep 17 00:00:00 2001
From: Alex Dutka <97711898+dutkalex at users.noreply.github.com>
Date: Thu, 23 Apr 2026 22:38:17 +0200
Subject: [PATCH 8/8] extend allDeclRefExpr impl to match structured bindings
references
---
clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
index c1c2db31e9c64..def63f8acc407 100644
--- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp
@@ -369,7 +369,7 @@ bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const ValueDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
auto Matches = match(
- findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
+ findAll(declRefExpr(to(valueDecl(equalsNode(&VarDecl)))).bind("declRef")),
Stmt, Context);
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
extractNodesByIdTo(Matches, "declRef", DeclRefs);
@@ -380,7 +380,7 @@ SmallPtrSet<const DeclRefExpr *, 16>
allDeclRefExprs(const ValueDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
auto Matches = match(
decl(forEachDescendant(
- declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
+ declRefExpr(to(valueDecl(equalsNode(&VarDecl)))).bind("declRef"))),
Decl, Context);
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
extractNodesByIdTo(Matches, "declRef", DeclRefs);
More information about the cfe-commits
mailing list