1*17d403f6SPiotr Zegar // RUN: %check_clang_tidy -std=c++20-or-later %s cppcoreguidelines-avoid-capturing-lambda-coroutines %t -- -- -isystem %S/Inputs/system
254178fc6SNoah Watkins 
354178fc6SNoah Watkins #include <coroutines.h>
454178fc6SNoah Watkins 
Caught()554178fc6SNoah Watkins void Caught() {
654178fc6SNoah Watkins     int v;
754178fc6SNoah Watkins 
854178fc6SNoah Watkins     [&] () -> task { int y = v; co_return; };
9*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
1054178fc6SNoah Watkins     [=] () -> task { int y = v; co_return; };
11*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
1254178fc6SNoah Watkins     [v] () -> task { co_return; };
13*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
1454178fc6SNoah Watkins     [&v] () -> task { co_return; };
15*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
1654178fc6SNoah Watkins     [y=v] () -> task { co_return; };
17*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
1854178fc6SNoah Watkins     [y{v}] () -> task { co_return; };
19*17d403f6SPiotr Zegar     // CHECK-MESSAGES: [[@LINE-1]]:5: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
2054178fc6SNoah Watkins }
2154178fc6SNoah Watkins 
2254178fc6SNoah Watkins struct S {
mS2354178fc6SNoah Watkins     void m() {
2454178fc6SNoah Watkins         [this] () -> task { co_return; };
25*17d403f6SPiotr Zegar         // CHECK-MESSAGES: [[@LINE-1]]:9: warning: coroutine lambda may cause use-after-free, avoid captures or ensure lambda closure object has guaranteed lifetime [cppcoreguidelines-avoid-capturing-lambda-coroutines]
2654178fc6SNoah Watkins     }
2754178fc6SNoah Watkins };
2854178fc6SNoah Watkins 
Safe()2954178fc6SNoah Watkins void Safe() {
3054178fc6SNoah Watkins     int v;
3154178fc6SNoah Watkins     [] () -> task { co_return; };
3254178fc6SNoah Watkins     [&] () -> task { co_return; };
3354178fc6SNoah Watkins     [=] () -> task { co_return; };
34*17d403f6SPiotr Zegar 
35*17d403f6SPiotr Zegar     [&v]{++v;}();
3654178fc6SNoah Watkins }
37