[clang-tools-extra] 0e2c5cd - [clang-tidy] Improve cppcoreguidelines-avoid-reference-coroutine-parameters check

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 24 14:49:46 PDT 2023


Author: Piotr Zegar
Date: 2023-08-24T21:49:28Z
New Revision: 0e2c5cda3cc713cac2cae651554fc070788760a6

URL: https://github.com/llvm/llvm-project/commit/0e2c5cda3cc713cac2cae651554fc070788760a6
DIFF: https://github.com/llvm/llvm-project/commit/0e2c5cda3cc713cac2cae651554fc070788760a6.diff

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

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

Fixes: #64915

Reviewed By: ccotter

Differential Revision: https://reviews.llvm.org/D158665

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
index 17e09c20836346..82e55cc2d28eae 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
@@ -16,17 +16,19 @@ namespace clang::tidy::cppcoreguidelines {
 
 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");
   }
 }

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7b00a0a6ddc7f6..1bc56023f142a1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -188,6 +188,12 @@ Changes in existing checks
   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.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp
index abeb7fc7701550..4df872ee15c6f4 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-reference-coroutine-parameters.cpp
+++ b/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 @@ void defines_a_lambda() {
   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;
+}


        


More information about the cfe-commits mailing list