[clang-tools-extra] [clang-tidy] Fix FP/FN in cppcoreguidelines-missing-std-forward (PR #178651)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 05:08:14 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: mitchell (zeyi2)
<details>
<summary>Changes</summary>
Closes #<!-- -->176873
---
Full diff: https://github.com/llvm/llvm-project/pull/178651.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp (+10-8)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+8)
- (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp (+46)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
index d1d81d510c8fb..77517a629030f 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MissingStdForwardCheck.cpp
@@ -107,19 +107,21 @@ void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) {
auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
isLambda(),
- hasParent(lambdaExpr(forCallable(equalsBoundNode("func")),
- anyOf(CapturedInCaptureList, CapturedInBody)))));
+ hasAncestor(lambdaExpr(forCallable(equalsBoundNode("func")),
+ anyOf(CapturedInCaptureList, CapturedInBody)))));
auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode("param")));
auto ForwardCallMatcher = callExpr(
callExpr().bind("call"), argumentCountIs(1),
- hasArgument(0, declRefExpr(to(varDecl().bind("var")))),
- forCallable(
- anyOf(allOf(equalsBoundNode("func"),
- functionDecl(hasAnyParameter(parmVarDecl(allOf(
- equalsBoundNode("param"), equalsBoundNode("var")))))),
- CapturedInLambda)),
+ hasArgument(
+ 0, declRefExpr(to(
+ varDecl(anyOf(equalsBoundNode("param"),
+ hasSameNameAsBoundNode("param"),
+ hasInitializer(ignoringParenImpCasts(
+ declRefExpr(to(equalsBoundNode("param")))))))
+ .bind("var")))),
+ forCallable(anyOf(equalsBoundNode("func"), CapturedInLambda)),
callee(unresolvedLookupExpr(hasAnyDeclaration(
namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 754880bd1a381..6faf18fc96b55 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -136,6 +136,14 @@ Changes in existing checks
the invalidating function in the warning message when a custom invalidation
function is used (via the `InvalidationFunctions` option).
+- Improved :doc:`cppcoreguidelines-missing-std-forward
+ <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by:
+
+ - Correctly handling forwarding in deeply nested lambdas.
+
+ - Fixing a false negative when multiple parameters are used in a lambda and
+ only some of them are forwarded.
+
- Improved :doc:`llvm-use-ranges
<clang-tidy/checks/llvm/use-ranges>` check by adding support for the following
algorithms: ``std::accumulate``, ``std::replace_copy``, and
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
index 98c592db7ce22..47e2977d6fc12 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/missing-std-forward.cpp
@@ -105,6 +105,26 @@ void foo(X &&x, Y &&y) {
use(y);
}
+template <typename T>
+void nested_but_no_forward(T &&arg) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: forwarding reference parameter 'arg' is never forwarded inside the function body [cppcoreguidelines-missing-std-forward]
+ [&]()
+ {
+ [&]()
+ { consumes_all(arg); }();
+ }();
+}
+
+template <typename T, typename U>
+void nested_forward_only_one(T &&arg1, U &&arg2) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: forwarding reference parameter 'arg2' is never forwarded inside the function body [cppcoreguidelines-missing-std-forward]
+ [&]()
+ {
+ [&]()
+ { consumes_all(std::forward<T>(arg1)); }();
+ }();
+}
+
} // namespace positive_cases
namespace negative_cases {
@@ -182,6 +202,32 @@ void lambda_value_reference_auxiliary_var(T&& t) {
[&x = t]() { T other = std::forward<T>(x); };
}
+template <typename T>
+void nested_forward(T &&arg) {
+ [&]()
+ {
+ [&]()
+ { consumes_all(std::forward<T>(arg)); }();
+ }();
+}
+
+template <typename T>
+void triple_nested_forward(T &&arg) {
+ [&]()
+ {
+ [&]()
+ {
+ [&]()
+ { consumes_all(std::forward<T>(arg)); }();
+ }();
+ }();
+}
+
+template <class T>
+void lambda_renamed_capture(T&& t) {
+ [&a = t]() { consumes_all(std::forward<T>(a)); };
+}
+
} // namespace negative_cases
namespace deleted_functions {
``````````
</details>
https://github.com/llvm/llvm-project/pull/178651
More information about the cfe-commits
mailing list