lambda_capture_local_by_ref.cpp 645 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // RUN: %clang++ -cc1 -verify %plugin_opts% %s 2>&1
  7. #include <AK/Function.h>
  8. void take_fn(Function<void()>) { }
  9. void take_fn_escaping(ESCAPING Function<void()>) { }
  10. void test()
  11. {
  12. // expected-note@+1 {{Annotate the variable declaration with IGNORE_USE_IN_ESCAPING_LAMBDA if it outlives the lambda}}
  13. int a = 0;
  14. take_fn([&a] {
  15. (void)a;
  16. });
  17. // expected-error@+1 {{Variable with local storage is captured by reference in a lambda marked ESCAPING}}
  18. take_fn_escaping([&a] {
  19. (void)a;
  20. });
  21. }