1*0e2c5cdaSPiotr Zegar // RUN: %check_clang_tidy -std=c++20 %s cppcoreguidelines-avoid-reference-coroutine-parameters %t --
2b06b248aSChris Cotter
3b06b248aSChris Cotter // NOLINTBEGIN
4b06b248aSChris Cotter namespace std {
5b06b248aSChris Cotter template <typename T, typename... Args>
6b06b248aSChris Cotter struct coroutine_traits {
7b06b248aSChris Cotter using promise_type = typename T::promise_type;
8b06b248aSChris Cotter };
9b06b248aSChris Cotter template <typename T = void>
10b06b248aSChris Cotter struct coroutine_handle;
11b06b248aSChris Cotter template <>
12b06b248aSChris Cotter struct coroutine_handle<void> {
13b06b248aSChris Cotter coroutine_handle() noexcept;
14b06b248aSChris Cotter coroutine_handle(decltype(nullptr)) noexcept;
15b06b248aSChris Cotter static constexpr coroutine_handle from_address(void*);
16b06b248aSChris Cotter };
17b06b248aSChris Cotter template <typename T>
18b06b248aSChris Cotter struct coroutine_handle {
19b06b248aSChris Cotter coroutine_handle() noexcept;
20b06b248aSChris Cotter coroutine_handle(decltype(nullptr)) noexcept;
21b06b248aSChris Cotter static constexpr coroutine_handle from_address(void*);
22b06b248aSChris Cotter operator coroutine_handle<>() const noexcept;
23b06b248aSChris Cotter };
24b06b248aSChris Cotter } // namespace std
25b06b248aSChris Cotter
26b06b248aSChris Cotter struct Awaiter {
27b06b248aSChris Cotter bool await_ready() noexcept;
28b06b248aSChris Cotter void await_suspend(std::coroutine_handle<>) noexcept;
29b06b248aSChris Cotter void await_resume() noexcept;
30b06b248aSChris Cotter };
31b06b248aSChris Cotter
32b06b248aSChris Cotter struct Coro {
33b06b248aSChris Cotter struct promise_type {
34b06b248aSChris Cotter Awaiter initial_suspend();
35b06b248aSChris Cotter Awaiter final_suspend() noexcept;
36b06b248aSChris Cotter void return_void();
37b06b248aSChris Cotter Coro get_return_object();
38b06b248aSChris Cotter void unhandled_exception();
39b06b248aSChris Cotter };
40b06b248aSChris Cotter };
41b06b248aSChris Cotter // NOLINTEND
42b06b248aSChris Cotter
43b06b248aSChris Cotter struct Obj {};
44b06b248aSChris Cotter
no_args()45b06b248aSChris Cotter Coro no_args() {
46b06b248aSChris Cotter co_return;
47b06b248aSChris Cotter }
48b06b248aSChris Cotter
no_references(int x,int * y,Obj z,const Obj w)49b06b248aSChris Cotter Coro no_references(int x, int* y, Obj z, const Obj w) {
50b06b248aSChris Cotter co_return;
51b06b248aSChris Cotter }
52b06b248aSChris Cotter
accepts_references(int & x,const int & y)53b06b248aSChris Cotter Coro accepts_references(int& x, const int &y) {
54b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
55b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-2]]:33: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
56b06b248aSChris Cotter co_return;
57b06b248aSChris Cotter }
58b06b248aSChris Cotter
accepts_references_and_non_references(int & x,int y)59b06b248aSChris Cotter Coro accepts_references_and_non_references(int& x, int y) {
60b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
61b06b248aSChris Cotter co_return;
62b06b248aSChris Cotter }
63b06b248aSChris Cotter
accepts_references_to_objects(Obj & x)64b06b248aSChris Cotter Coro accepts_references_to_objects(Obj& x) {
65b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
66b06b248aSChris Cotter co_return;
67b06b248aSChris Cotter }
68b06b248aSChris Cotter
non_coro_accepts_references(int & x)69b06b248aSChris Cotter Coro non_coro_accepts_references(int& x) {
70b06b248aSChris Cotter if (x);
71b06b248aSChris Cotter return Coro{};
72b06b248aSChris Cotter }
73b06b248aSChris Cotter
defines_a_lambda()74b06b248aSChris Cotter void defines_a_lambda() {
75b06b248aSChris Cotter auto NoArgs = [](int x) -> Coro { co_return; };
76b06b248aSChris Cotter
77b06b248aSChris Cotter auto NoReferences = [](int x) -> Coro { co_return; };
78b06b248aSChris Cotter
79b06b248aSChris Cotter auto WithReferences = [](int& x) -> Coro { co_return; };
80b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
81b06b248aSChris Cotter
82b06b248aSChris Cotter auto WithReferences2 = [](int&) -> Coro { co_return; };
83b06b248aSChris Cotter // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
84b06b248aSChris Cotter }
85*0e2c5cdaSPiotr Zegar
coroInFunctionWithReference(int &)86*0e2c5cdaSPiotr Zegar void coroInFunctionWithReference(int&) {
87*0e2c5cdaSPiotr Zegar auto SampleCoro = [](int x) -> Coro { co_return; };
88*0e2c5cdaSPiotr Zegar }
89*0e2c5cdaSPiotr Zegar
lambdaWithReferenceInCoro()90*0e2c5cdaSPiotr Zegar Coro lambdaWithReferenceInCoro() {
91*0e2c5cdaSPiotr Zegar auto SampleLambda = [](int& x) {};
92*0e2c5cdaSPiotr Zegar co_return;
93*0e2c5cdaSPiotr Zegar }
94*0e2c5cdaSPiotr Zegar
95*0e2c5cdaSPiotr Zegar using MyIntegerRef = int&;
coroWithReferenceBehindTypedef(MyIntegerRef ref)96*0e2c5cdaSPiotr Zegar Coro coroWithReferenceBehindTypedef(MyIntegerRef ref) {
97*0e2c5cdaSPiotr Zegar // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: coroutine parameters should not be references [cppcoreguidelines-avoid-reference-coroutine-parameters]
98*0e2c5cdaSPiotr Zegar co_return;
99*0e2c5cdaSPiotr Zegar }
100