https://github.com/steeno17 created 
https://github.com/llvm/llvm-project/pull/246912

`AllowedTypes` entries containing template arguments, such as 
`Foo<std::basic_string<char>>` (from the issue), never matched class template 
specializations, because names were compared without their template arguments.

Entries that have `<`, are now also matched against the full name of a 
specialization and its template arguments. Entries without `<` behave the same 
as before.

With the configuration from the issue:
(`AllowedTypes: 'Foo<std::basic_string<char>>;Foo<std::string>'`), the example 
no longer warns.

Fixes #64842.

>From 08de9f58ee618761d622080ae44a51aec37e019d Mon Sep 17 00:00:00 2001
From: steeno17 <[email protected]>
Date: Sun, 29 Nov 2026 01:20:10 +0000
Subject: [PATCH] [clang-tidy] `AllowedTypes` entries containing template
 arguments, such as `Foo<std::basic_string<char>>` (from the issue), never
 matched class template specializations, because names were compared without
 their template arguments.

Entries that have `<`, are now also matched against the full name of a 
specialization and its template arguments. Entries without `<` behave the same 
as before.

With the configuration from the issue:
(`AllowedTypes: 'Foo<std::basic_string<char>>;Foo<std::string>'`), the example 
no longer warns.

Fixes #64842.
---
 clang-tools-extra/clang-tidy/utils/Matchers.h | 29 ++++++++++++---
 clang-tools-extra/docs/ReleaseNotes.md        |  5 +++
 .../performance/unnecessary-value-param.rst   |  5 +++
 ...ary-value-param-allowed-template-types.cpp | 37 +++++++++++++++++++
 4 files changed, 71 insertions(+), 5 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-allowed-template-types.cpp

diff --git a/clang-tools-extra/clang-tidy/utils/Matchers.h 
b/clang-tools-extra/clang-tidy/utils/Matchers.h
index 852c3f9295fa08..4db3adb6daf548 100644
--- a/clang-tools-extra/clang-tidy/utils/Matchers.h
+++ b/clang-tools-extra/clang-tidy/utils/Matchers.h
@@ -10,8 +10,10 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_MATCHERS_H
 
 #include "TypeTraits.h"
+#include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ExprConcepts.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
+#include "llvm/Support/raw_ostream.h"
 #include <optional>
 
 namespace clang::tidy::matchers {
@@ -112,22 +114,39 @@ class MatchesAnyListedRegexNameMatcher
       MatchFullyQualified,
     };
     MatchMode Mode;
+    bool HasTemplateArgs;
 
   public:
     NameMatcher(const StringRef Regex)
-        : Regex(Regex), Mode(determineMatchMode(Regex)) {}
+        : Regex(Regex), Mode(determineMatchMode(Regex)),
+          HasTemplateArgs(Regex.contains('<')) {}
 
     bool match(const NamedDecl &ND) const {
       switch (Mode) {
       case MatchMode::MatchQualified:
-        return Regex.match(ND.getQualifiedNameAsString());
+        if (Regex.match(ND.getQualifiedNameAsString()))
+          return true;
+        break;
       case MatchMode::MatchFullyQualified:
-        return Regex.match("::" + ND.getQualifiedNameAsString());
+        if (Regex.match("::" + ND.getQualifiedNameAsString()))
+          return true;
+        break;
       default:
         if (const IdentifierInfo *II = ND.getIdentifier())
-          return Regex.match(II->getName());
-        return false;
+          if (Regex.match(II->getName()))
+            return true;
+        break;
       }
+
+      if (!HasTemplateArgs || !isa<ClassTemplateSpecializationDecl>(ND))
+        return false;
+
+      std::string Printed;
+      llvm::raw_string_ostream OS(Printed);
+
+      ND.getNameForDiagnostic(OS, ND.getASTContext().getPrintingPolicy(), 
true);
+      return Regex.match(Mode == MatchMode::MatchFullyQualified ? "::" + 
Printed
+                                                                : Printed);
     }
 
   private:
diff --git a/clang-tools-extra/docs/ReleaseNotes.md 
b/clang-tools-extra/docs/ReleaseNotes.md
index 7f3eab9f64e3e1..1c041e54c66ad2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -409,6 +409,11 @@ infrastructure are described first, followed by 
tool-specific sections.
   avoid offering fix-its for string literals originating from macro
   expansions.
 
+- Improved {doc}`performance-unnecessary-value-param
+  <clang-tidy/checks/performance/unnecessary-value-param>` check by allowing
+  class template specializations to be matched by the {option}`AllowedTypes`
+  option when an entry includes template arguments.
+
 - Improved {doc}`readability-convert-member-functions-to-static
   <clang-tidy/checks/readability/convert-member-functions-to-static>` check by
   fixing a crash when checking a const-qualified method declared with the
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst
index cd25d7d94d99b0..94b81942ef0870 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst
@@ -75,6 +75,11 @@ Options
    matched against the qualified type name (i.e. ``namespace::Type``),
    otherwise it is matched against only the type name (i.e. ``Type``).
 
+   A name that contains template arguments is also matched against the name of
+   a class template specialization including its template arguments. For 
example,
+   ``Foo<std::basic_string<char>>`` matches a parameter of type 
+   ``Foo<std::string>``, while ``Foo<std::string>`` does not.
+
 .. option:: IgnoreCoroutines
 
    A boolean specifying whether the check should suggest passing parameters by
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-allowed-template-types.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-allowed-template-types.cpp
new file mode 100644
index 00000000000000..bd419931e2fdbc
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-allowed-template-types.cpp
@@ -0,0 +1,37 @@
+// RUN: %check_clang_tidy %s performance-unnecessary-value-param %t -- \
+// RUN:   -config="{CheckOptions: 
{performance-unnecessary-value-param.AllowedTypes: 
'Allowed<Expensive>;Regexed<.*>;Expensive'}}" --
+
+struct Expensive {
+  Expensive();
+  Expensive(const Expensive &);
+  ~Expensive();
+};
+
+struct Other {
+  Other();
+  Other(const Other &);
+  ~Other();
+};
+
+template <typename T> struct Allowed { T s; };
+template <typename T> struct Regexed { T s; };
+template <typename T> struct Guarded { T s; };
+
+// Exact specialization in AllowedTypes: no warning.
+void allowedSpecialization(Allowed<Expensive> v) { (void)v.s; }
+
+// Same template, different argument: still warns.
+void differentSpecialization(Allowed<Other> v) { (void)v.s; }
+// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: the parameter 'v' of type 
'Allowed<Other>' is copied for each invocation but only used as a const 
reference; consider making it a const reference 
[performance-unnecessary-value-param]
+// CHECK-FIXES: void differentSpecialization(const Allowed<Other>& v) { 
(void)v.s; }
+
+// Regex entry containing template arguments: no warning for either argument.
+void regexSpecializationA(Regexed<Expensive> v) { (void)v.s; }
+void regexSpecializationB(Regexed<Other> v) { (void)v.s; }
+
+// Regression guard: the bare entry 'Expensive' must not suppress a
+// specialization that merely mentions it as an argument.
+void guarded(Guarded<Expensive> v) { (void)v.s; }
+// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: the parameter 'v' of type 
'Guarded<Expensive>' is copied for each invocation but only used as a const 
reference; consider making it a const reference 
[performance-unnecessary-value-param]
+// CHECK-FIXES: void guarded(const Guarded<Expensive>& v) { (void)v.s; }
+

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to