[clang-tools-extra] [clang-tidy] Fix unnecessary-value-param AllowedTypes for specializations (PR #246912)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 14:56:42 PST 2026
https://github.com/steeno17 updated https://github.com/llvm/llvm-project/pull/246912
>From 08de9f58ee618761d622080ae44a51aec37e019d Mon Sep 17 00:00:00 2001
From: steeno17 <254257529+steeno17 at users.noreply.github.com>
Date: Sun, 29 Nov 2026 01:20:10 +0000
Subject: [PATCH 1/3] [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; }
+
>From cfb3f7280ef656d64f336cf2d4cdc90478001d14 Mon Sep 17 00:00:00 2001
From: steeno17 <254257529+steeno17 at users.noreply.github.com>
Date: Fri, 4 Dec 2026 22:51:53 +0000
Subject: [PATCH 2/3] Merged nested if statements
---
clang-tools-extra/clang-tidy/utils/Matchers.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/Matchers.h b/clang-tools-extra/clang-tidy/utils/Matchers.h
index 4db3adb6daf548..2ba7572cd1c35a 100644
--- a/clang-tools-extra/clang-tidy/utils/Matchers.h
+++ b/clang-tools-extra/clang-tidy/utils/Matchers.h
@@ -132,9 +132,9 @@ class MatchesAnyListedRegexNameMatcher
return true;
break;
default:
- if (const IdentifierInfo *II = ND.getIdentifier())
- if (Regex.match(II->getName()))
- return true;
+ if (const IdentifierInfo *II = ND.getIdentifier();
+ II && Regex.match(II->getName()))
+ return true;
break;
}
>From 9af2671b00dff1ab5cb69e0a523c76c48edea238 Mon Sep 17 00:00:00 2001
From: steeno17 <254257529+steeno17 at users.noreply.github.com>
Date: Fri, 4 Dec 2026 22:56:29 +0000
Subject: [PATCH 3/3] Linter issue fixed
---
.../clang-tidy/checks/performance/unnecessary-value-param.rst | 2 +-
.../unnecessary-value-param-allowed-template-types.cpp | 1 -
2 files changed, 1 insertion(+), 2 deletions(-)
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 94b81942ef0870..1aa17334628adb 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
@@ -77,7 +77,7 @@ Options
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::basic_string<char>>`` matches a parameter of type
``Foo<std::string>``, while ``Foo<std::string>`` does not.
.. option:: IgnoreCoroutines
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
index bd419931e2fdbc..b1da207c8e82ed 100644
--- 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
@@ -34,4 +34,3 @@ void regexSpecializationB(Regexed<Other> v) { (void)v.s; }
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; }
-
More information about the cfe-commits
mailing list