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"); } }
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"); } }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits