mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 12:37:44 +00:00
AK+Everywhere: Disallow returning a reference from a fallible expression
This will silently make a copy. Rather than masking this behavior, let's explicitly disallow it.
This commit is contained in:
parent
3de75f6436
commit
afc0e461e1
11 changed files with 86 additions and 54 deletions
38
AK/Try.h
38
AK/Try.h
|
@ -15,22 +15,30 @@
|
|||
// on statement expressions [1]. This is known to be implemented
|
||||
// by at least clang and gcc.
|
||||
// [1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
|
||||
//
|
||||
// If the static_assert below is triggered, it means you tried to return a reference
|
||||
// from a fallible expression. This will not do what you want; the statement expression
|
||||
// will create a copy regardless, so it is explicitly disallowed.
|
||||
|
||||
#define TRY(expression) \
|
||||
({ \
|
||||
/* Ignore -Wshadow to allow nesting the macro. */ \
|
||||
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
|
||||
auto _temporary_result = (expression)); \
|
||||
if (_temporary_result.is_error()) [[unlikely]] \
|
||||
return _temporary_result.release_error(); \
|
||||
_temporary_result.release_value(); \
|
||||
#define TRY(expression) \
|
||||
({ \
|
||||
/* Ignore -Wshadow to allow nesting the macro. */ \
|
||||
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
|
||||
auto _temporary_result = (expression)); \
|
||||
static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
|
||||
"Do not return a reference from a fallible expression"); \
|
||||
if (_temporary_result.is_error()) [[unlikely]] \
|
||||
return _temporary_result.release_error(); \
|
||||
_temporary_result.release_value(); \
|
||||
})
|
||||
|
||||
#define MUST(expression) \
|
||||
({ \
|
||||
/* Ignore -Wshadow to allow nesting the macro. */ \
|
||||
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
|
||||
auto _temporary_result = (expression)); \
|
||||
VERIFY(!_temporary_result.is_error()); \
|
||||
_temporary_result.release_value(); \
|
||||
#define MUST(expression) \
|
||||
({ \
|
||||
/* Ignore -Wshadow to allow nesting the macro. */ \
|
||||
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
|
||||
auto _temporary_result = (expression)); \
|
||||
static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
|
||||
"Do not return a reference from a fallible expression"); \
|
||||
VERIFY(!_temporary_result.is_error()); \
|
||||
_temporary_result.release_value(); \
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue