Author: Aditya Singh
Date: 2026-03-07T22:04:58+08:00
New Revision: 818168ebca24640f8cfc495abc79f658e43c7e2c

URL: 
https://github.com/llvm/llvm-project/commit/818168ebca24640f8cfc495abc79f658e43c7e2c
DIFF: 
https://github.com/llvm/llvm-project/commit/818168ebca24640f8cfc495abc79f658e43c7e2c.diff

LOG: [clang-tidy] Fix false positive for constrained template parameters in 
`cppcoreguidelines-missing-std-forward` (#182038)

Explicit object parameters with a type constraint are skipped to avoid
false positives. They are technically still forwarding references per
[temp.deduct.call], but rarely intended to be perfectly forwarded.

Fixes #180362

Added: 
    
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward-cxx23.cpp

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst

Removed: 
    


################################################################################
diff  --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index d1d81d510c8fb..3fda2a671eaf4 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -46,6 +46,16 @@ AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) {
 
   const QualType ParamType =
       Node.getType().getNonPackExpansionType()->getPointeeType();
+
+  // Explicit object parameters with a type constraint are still forwarding
+  // references per [temp.deduct.call]. We conservatively suppress warnings
+  // here to avoid false positives when constraints restrict the deduced type,
+  // accepting false negatives as a trade-off.
+  if (Node.isExplicitObjectParameter())
+    if (const auto *TTPT = ParamType->getAs<TemplateTypeParmType>())
+      if (const auto *Decl = TTPT->getDecl(); Decl && 
Decl->hasTypeConstraint())
+        return false;
+
   const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
   if (!TemplateType)
     return false;

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b0b4cd646c3bd..9f4d7e6923fa0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -216,6 +216,10 @@ Changes in existing checks
   <clang-tidy/checks/cppcoreguidelines/init-variables>` check by ensuring that
   member pointers are correctly flagged as uninitialized.
 
+- Improved :doc:`cppcoreguidelines-missing-std-forward
+  <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by fixing
+  a false positive for constrained template parameters.
+
 - Improved :doc:`cppcoreguidelines-pro-type-vararg
   <clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check by no longer
   warning on builtins with custom type checking (e.g., type-generic builtins

diff  --git 
a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst
index c2af1239a7d4c..c8c0206a4d67c 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/missing-std-forward.rst
@@ -45,3 +45,10 @@ Options
 This check implements `F.19
 <http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rf-forward>`_
 from the C++ Core Guidelines.
+
+Limitations
+-----------
+
+Explicit object parameters (``this Self&&``) with a type constraint are not
+checked to avoid false positives. Such parameters are rarely intended to be
+perfectly forwarded.

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward-cxx23.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward-cxx23.cpp
new file mode 100644
index 0000000000000..723b7893673a1
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward-cxx23.cpp
@@ -0,0 +1,58 @@
+// RUN: %check_clang_tidy -std=c++23-or-later %s 
cppcoreguidelines-missing-std-forward %t -- -- -fno-delayed-template-parsing
+
+// NOLINTBEGIN
+namespace std {
+
+template <typename T> struct remove_reference      { using type = T; };
+template <typename T> struct remove_reference<T&>  { using type = T; };
+template <typename T> struct remove_reference<T&&> { using type = T; };
+
+template <typename T> using remove_reference_t = typename 
remove_reference<T>::type;
+
+template <typename T> constexpr T &&forward(remove_reference_t<T> &t) noexcept;
+template <typename T> constexpr T &&forward(remove_reference_t<T> &&t) 
noexcept;
+template <typename T> constexpr remove_reference_t<T> &&move(T &&x);
+
+template <class T, class U>
+concept derived_from = true;
+
+template <class T>
+concept integral = true;
+
+} // namespace std
+// NOLINTEND
+
+// Tests for constrained explicit object parameters (GH#180362).
+
+// Constrained explicit object parameter is not a forwarding reference.
+class GH180362_A {
+public:
+  template <std::derived_from<GH180362_A> Self>
+  auto operator|(this Self &&self, int) -> void {}
+};
+
+// Another constrained explicit object parameter — no warning.
+struct GH180362_C {
+  template <std::integral Self>
+  void bar(this Self &&self) {}
+};
+
+// Unconstrained explicit object parameter IS a forwarding reference.
+struct GH180362_B {
+  template <typename Self>
+  void foo(this Self &&self) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: forwarding reference parameter 
'self' is never forwarded inside the function body 
[cppcoreguidelines-missing-std-forward]
+};
+
+// Requires-clause on explicit object parameter is NOT a type constraint.
+struct GH180362_D {
+  template <typename Self>
+    requires std::derived_from<Self, GH180362_D>
+  void baz(this Self &&self) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: forwarding reference parameter 
'self' is never forwarded inside the function body 
[cppcoreguidelines-missing-std-forward]
+};
+
+// Constrained non-explicit-object parameter IS still a forwarding reference.
+template <std::integral T>
+void gh180362_takes_integral(T &&t) {}
+// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: forwarding reference parameter 
't' is never forwarded inside the function body 
[cppcoreguidelines-missing-std-forward]


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

Reply via email to