[PATCH] D137514: [clang-tidy] add check for capturing lambda coroutines

2022-11-06 Thread Noah Watkins via Phabricator via cfe-commits
dotnwat created this revision.
Herald added subscribers: carlosgalvezp, ChuanqiXu, kbarton, xazax.hun, 
nemanjai.
Herald added a project: All.
dotnwat requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Signed-off-by: Noah Watkins 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137514

Files:
  
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCapturingLambdaCoroutinesCheck.cpp
  
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCapturingLambdaCoroutinesCheck.h
  clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt
  clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-capturing-lambda-coroutines.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-capturing-lambda-coroutines.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-capturing-lambda-coroutines.cpp
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-capturing-lambda-coroutines.cpp
@@ -0,0 +1,35 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s cppcoreguidelines-avoid-capturing-lambda-coroutines %t -- -- \
+// RUN:   -isystem %S/readability/Inputs/identifier-naming/system
+
+#include 
+
+void Caught() {
+int v;
+
+[&] () -> task { int y = v; co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+[=] () -> task { int y = v; co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+[v] () -> task { co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+[&v] () -> task { co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+[y=v] () -> task { co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+[y{v}] () -> task { co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:5: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+}
+
+struct S {
+void m() {
+[this] () -> task { co_return; };
+// CHECK-MESSAGES: [[@LINE-1]]:9: warning: found capturing coroutine lambda [cppcoreguidelines-avoid-capturing-lambda-coroutines]
+}
+};
+
+void Safe() {
+int v;
+[] () -> task { co_return; };
+[&] () -> task { co_return; };
+[=] () -> task { co_return; };
+}
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -177,6 +177,7 @@
`clang-analyzer-valist.Unterminated `_,
`concurrency-mt-unsafe `_,
`concurrency-thread-canceltype-asynchronous `_,
+   `cppcoreguidelines-avoid-capturing-lambda-coroutines `_, "Yes"
`cppcoreguidelines-avoid-const-or-ref-data-members `_,
`cppcoreguidelines-avoid-do-while `_,
`cppcoreguidelines-avoid-goto `_,
Index: clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-capturing-lambda-coroutines.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-avoid-capturing-lambda-coroutines.rst
@@ -0,0 +1,31 @@
+.. title:: clang-tidy - cppcoreguidelines-avoid-capturing-lambda-coroutines
+
+cppcoreguidelines-avoid-capturing-lambda-coroutines
+===
+
+Warns if a capturing lambda is a coroutine. For example:
+
+.. code-block:: c++
+
+   int c;
+
+   [c] () -> task { co_return; };
+   [&] () -> task { int y = c; co_return; };
+   [=] () -> task { int y = c; co_return; };
+
+   struct s {
+   void m() {
+   [this] () -> task { co_return; };
+   }
+   };
+
+All of the cases above will trigger the warning. However, implicit captures
+do not trigger the warning unless the body of the lambda uses the capture.
+For example, the following do not trigger the warning.
+
+.. code-block:: c++
+
+   int c;
+
+   [&] () -> task { co_return; };
+   [=] () -> task { co_return; };
Index: clang-tools-extra/docs/ReleaseNotes.rst
===
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -115,6 +115,13 @@
 
   Warns when using ``do-while`` loops.
 
+- New :doc:`cppcoreguidelines-avoid-capturing-lambda-corou

[PATCH] D137514: [clang-tidy] add check for capturing lambda coroutines

2022-11-07 Thread Noah Watkins via Phabricator via cfe-commits
dotnwat added a comment.

In D137514#3911046 , @ChuanqiXu wrote:

> This is helpful. I feel it is OK to add the warning in the clang too. Are you 
> interested in that?

Yes, but unfortunately I'm very time constrained until the end of the year. Is 
that something that would need to be added to this diff, or could be separate? 
(fyi, brand new to clang development)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137514/new/

https://reviews.llvm.org/D137514

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits