[clang-tools-extra] [clang-tidy][readability-identifier-length] Add a line count threshold (without std::transform_reduce) (PR #193276)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 21 11:52:02 PDT 2026
================
@@ -85,6 +90,49 @@ void IdentifierLengthCheck::registerMatchers(MatchFinder *Finder) {
this);
}
+static std::optional<unsigned> countLinesToLastUse(const VarDecl *Var,
+ const SourceManager *SrcMgr,
+ ASTContext *Ctx) {
+ const auto *ParentScope = llvm::dyn_cast<FunctionDecl>(Var->getDeclContext());
+ if (ParentScope == nullptr)
+ return std::nullopt;
+
+ auto AllRefs =
+ utils::decl_ref_expr::allDeclRefExprs(*Var, *ParentScope, *Ctx);
+
+ auto AllRefLines =
+ llvm::map_range(AllRefs, [&](const DeclRefExpr *RefToVar) -> unsigned {
+ return SrcMgr->getSpellingLineNumber(RefToVar->getLocation());
+ });
+
+ const unsigned DeclLine = SrcMgr->getSpellingLineNumber(Var->getLocation());
+ const unsigned LastUseLine =
+ AllRefLines.empty() ? DeclLine
+ : std::max(DeclLine, *llvm::max_element(AllRefLines));
+
+ // const unsigned LastUseLine = std::transform_reduce(
+ // AllRefs.begin(), AllRefs.end(), DeclLine,
+ // [](unsigned Lhs, unsigned Rhs) -> unsigned { return std::max(Lhs, Rhs);
+ // },
+ // [&](const DeclRefExpr *RefToVar) -> unsigned {
+ // return SrcMgr->getSpellingLineNumber(RefToVar->getLocation());
+ // });
----------------
vbvictor wrote:
We should remove these comments then
https://github.com/llvm/llvm-project/pull/193276
More information about the cfe-commits
mailing list