[clang-tools-extra] [clang-tidy] Qualify the AllowExplicitObjectParameters rationale (PR #242976)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 15 22:55:59 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Cyrus Ding (dingcyrus)

<details>
<summary>Changes</summary>

Fixes #<!-- -->242802

The option's rationale says explicit object parameters are exempted "because the captures are moved into the coroutine frame, decoupling their lifetime from the lambda object". That only holds once the call operator has been invoked: for a by-value `this auto` the closure is copied into the frame *at the call*, so the lambda object must still be alive at that point. A receiver that keeps only a reference to the lambda and calls it later, such as a coroutine taking `F&&`, copies from a destroyed closure, and the matcher — `unless(hasDeducingThis())`, which exempts every lambda whose call operator `isExplicitObjectMemberFunction()` — stays silent about it when the option is on.

This documents the limitation instead of changing the heuristic. Narrowing the matcher to keep warning in that case would itself be heuristic; #<!-- -->221750 asks for that on the `this auto&` case.

Docs only, no code or test change. Checked the claim is not repeated elsewhere in clang-tools-extra, and verified the RST with a differential docutils parse of the file (no new warnings; the single Sphinx-directive error is pre-existing).

AI assistance was used for source analysis and local verification.


---
Full diff: https://github.com/llvm/llvm-project/pull/242976.diff


1 Files Affected:

- (modified) clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines.rst (+7) 


``````````diff
diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines.rst
index f54b33c0a7de83..17c1ceb16a9a67 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capturing-lambda-coroutines.rst
@@ -63,6 +63,13 @@ Options
   check, because the captures are moved into the coroutine frame, decoupling
   their lifetime from the lambda object.
 
+  This exemption is a heuristic. The closure object is copied into the frame
+  when the call operator is *invoked*, so the lambda object must still be alive
+  at that point. Code that keeps only a reference to the lambda and calls it
+  later, such as a coroutine that takes a forwarding reference ``F&&``, still
+  copies from a destroyed closure and is not made safe by an explicit object
+  parameter.
+
   Default is `false`.
 
   The example from above can be made safe and will pass this check with the

``````````

</details>


https://github.com/llvm/llvm-project/pull/242976


More information about the cfe-commits mailing list