[PATCH] D158665: [clang-tidy] Improve cppcoreguidelines-avoid-reference-coroutine-parameters check

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 13:12:40 PDT 2023


PiotrZSL created this revision.
PiotrZSL added reviewers: carlosgalvezp, njames93, ccotter.
Herald added subscribers: ChuanqiXu, shchenz, kbarton, xazax.hun, nemanjai.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Ignore false positives related to matching parameters of non
coroutine functions and increase issue detection for cases
involving type aliases with references.

Fixes: #64915


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158665

Files:
  clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy -std=c++20 %s cppcoreguidelines-avoid-reference-coroutine-parameters %t
+// RUN: %check_clang_tidy -std=c++20 %s cppcoreguidelines-avoid-reference-coroutine-parameters %t --
 
 // NOLINTBEGIN
 namespace std {
@@ -82,3 +82,18 @@
   auto WithReferences2 = [](int&) -> Coro { co_return; };
   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
 }
+
+void coroInFunctionWithReference(int&) {
+  auto SampleCoro = [](int x) -> Coro { co_return; };
+}
+
+Coro lambdaWithReferenceInCoro() {
+  auto SampleLambda = [](int& x) {};
+  co_return;
+}
+
+using MyIntegerRef = int&;
+Coro coroWithReferenceBehindTypedef(MyIntegerRef ref) {
+// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
+  co_return;
+}
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -188,6 +188,12 @@
   to ignore ``static`` variables declared within the scope of
   ``class``/``struct``.
 
+- Improved :doc:`cppcoreguidelines-avoid-reference-coroutine-parameters
+  <clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters>`
+  check to ignore false positives related to matching parameters of non
+  coroutine functions and increase issue detection for cases involving type
+  aliases with references.
+
 - Improved :doc:`cppcoreguidelines-prefer-member-initializer
   <clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
   ignore delegate constructors.
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
@@ -16,17 +16,19 @@
 
 void AvoidReferenceCoroutineParametersCheck::registerMatchers(
     MatchFinder *Finder) {
-  auto IsCoroMatcher =
-      hasDescendant(expr(anyOf(coyieldExpr(), coreturnStmt(), coawaitExpr())));
-  Finder->addMatcher(parmVarDecl(hasType(type(referenceType())),
-                                 hasAncestor(functionDecl(IsCoroMatcher)))
-                         .bind("param"),
-                     this);
+  Finder->addMatcher(
+      functionDecl(unless(parameterCountIs(0)), hasBody(coroutineBodyStmt()))
+          .bind("fnt"),
+      this);
 }
 
 void AvoidReferenceCoroutineParametersCheck::check(
     const MatchFinder::MatchResult &Result) {
-  if (const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param")) {
+  const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("fnt");
+  for (const ParmVarDecl *Param : Function->parameters()) {
+    if (!Param->getType().getCanonicalType()->isReferenceType())
+      continue;
+
     diag(Param->getBeginLoc(), "coroutine parameters should not be references");
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158665.552863.patch
Type: text/x-patch
Size: 3552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230823/b6a9abba/attachment-0001.bin>


More information about the cfe-commits mailing list