ladybird/Tests/ClangPlugins/LambdaTests/lambda_capture_local_by_ref.cpp
Matthew Olsson e0d6afbabe ClangPlugins: Invert the lambda detection escape mechanism
Instead of being opt-out with NOESCAPE, it is now opt-in with ESCAPING.
Opt-out is ideal, but unfortunately this was extremely noisy when
compiling the entire codebase. Escaping functions are rarer than non-
escaping ones, so let's just go with that for now.

This also allows us to gradually add heuristics for detecting missing
ESCAPING annotations and emitting them as errors. It also nicely matches
the spelling that Swift uses (@escaping), which is where this idea
originally came from.
2024-05-22 21:55:34 -06:00

27 lines
647 B
C++

/*
* Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
// RUN: %clang++ -cc1 -verify %plugin_opts% %s 2>&1
#include <AK/Function.h>
void take_fn(Function<void()>) { }
void take_fn_escaping(ESCAPING Function<void()>) { }
void test()
{
// expected-note@+1 {{Annotate the variable declaration with IGNORE_USE_IN_ESCAPING_LAMBDA if it outlives the lambda}}
int a = 0;
take_fn([&a] {
(void)a;
});
// expected-warning@+1 {{Variable with local storage is captured by reference in a lambda marked ESCAPING}}
take_fn_escaping([&a] {
(void)a;
});
}