[clang-tools-extra] [clang-tidy] Fix false-positive in misc-const-correctness with empty … (PR #235908)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 13:48:19 PDT 2026


https://github.com/Abhi10111 updated https://github.com/llvm/llvm-project/pull/235908

>From cfa57e19c697293845f0d9434dc5d0f6bee82cac Mon Sep 17 00:00:00 2001
From: Abhishek <abhishekaggarwal896 at gmail.com>
Date: Tue, 27 Oct 2026 01:59:31 +0530
Subject: [PATCH] [clang-tidy] Fix false-positive in misc-const-correctness
 with empty fold expressions

In function templates where a variable is referenced inside a fold expression,
instantiating the template with an empty parameter pack collapses the fold
expression to an empty statement in the instantiation's AST. As a result,
misc-const-correctness mistakenly reported that the variable was never mutated
and suggested adding `const` to the template definition.

This patch inspects the template instantiation pattern and suppresses the
warning if the variable participates in a fold expression within the pattern.

Fixes #235629

Signed-off-by: Abhishek <abhishek at beans.ai>
---
 .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 22 +++++++++++++++++++
 clang-tools-extra/docs/ReleaseNotes.md        |  4 ++++
 .../misc/const-correctness-templates.cpp      | 18 +++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 4532a27962af29..3796a61bd1b118 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -36,6 +36,9 @@ AST_MATCHER_P(DeclStmt, containsAnyDeclaration,
              InnerMatcher, Node.decl_begin(), Node.decl_end(), Finder,
              Builder) != Node.decl_end();
 }
+AST_MATCHER_P(Decl, isAtLocation, SourceLocation, Loc) {
+  return Node.getLocation() == Loc;
+}
 AST_MATCHER(ReferenceType, isSpelledAsLValue) {
   return Node.isSpelledAsLValue();
 }
@@ -199,6 +202,21 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
   }
 }
 
+static bool isPartOfFoldExprInTemplate(const VarDecl *Variable,
+                                       const FunctionDecl *Function,
+                                       ASTContext &Context) {
+  const FunctionDecl *Pattern = Function->getTemplateInstantiationPattern();
+  if (!Pattern)
+    return false;
+
+  const Stmt *Body = Pattern->getBody();
+  if (!Body)
+    return false;
+  const auto FoldMatcher = stmt(hasDescendant(cxxFoldExpr(hasDescendant(
+      declRefExpr(to(varDecl(isAtLocation(Variable->getLocation()))))))));
+  return !match(FoldMatcher, *Body, Context).empty();
+}
+
 static bool isStdMoveOperandInTemplate(const VarDecl *Variable,
                                        const FunctionDecl *Func,
                                        ASTContext &Context) {
@@ -289,6 +307,10 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
       isStdMoveOperandInTemplate(Variable, Function, *Result.Context))
     return;
 
+  if (IsNormalVariableInTemplate &&
+      isPartOfFoldExprInTemplate(Variable, Function, *Result.Context))
+    return;
+
   if (IsNormalVariableInTemplate &&
       TemplateDiagnosticsCache.contains(Variable->getBeginLoc()))
     return;
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 310b084c04eb04..0a17ad1c8111cd 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -273,6 +273,10 @@ infrastructure are described first, followed by tool-specific sections.
   - No longer diagnoses variables used as operands of `std::move` in template
     instantiations, where the suggested `const` would break the move for
     instantiations with real move semantics.
+  
+  - No longer diagnoses variables used in fold expressions in template
+    instantiations where the parameter pack is empty, since adding const 
+    would break the case where the pack is non empty.
 
 - Fixed an infinite loop in {doc}`misc-multiple-inheritance
   <clang-tidy/checks/misc/multiple-inheritance>` when checking a class that
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
index 624159210698c5..62d8caca98b6fa 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
@@ -99,3 +99,21 @@ void f() {
 }
 
 } // namespace gh60895
+
+namespace gh235629 {
+
+struct A {
+  void add(int);
+};
+
+template <int... Args>
+void fun() {
+  A a;
+  (a.add(Args), ...);
+}
+
+void f() {
+  fun();
+}
+
+} // namespace gh235629
\ No newline at end of file



More information about the cfe-commits mailing list