Skip to content

Commit 6bdcf9c

Browse files
rikvanrielmeta-codesync[bot]
authored andcommitted
annotate value()/operator*/operator->
Summary: Standalone CI probe, on clean master. Adds [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] to folly::Optional::value()/operator*/operator-> so the compiler (-Wdangling / -Wreturn-stack-address) catches consumers that bind a reference/pointer into an Optional that does not outlive it (e.g. `const T& x = getOpt().value();` on a temporary). Reviewed By: r-barnes Differential Revision: D109982007 fbshipit-source-id: 42dc6c558c7d71a51007d6a1ee1e5a85928b033b
1 parent ff48b1e commit 6bdcf9c

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

‎folly/Optional.h‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include <type_traits>
6969
#include <utility>
7070

71+
#include <folly/CppAttributes.h>
7172
#include <folly/Portability.h>
7273
#include <folly/Traits.h>
7374
#include <folly/Utility.h>
@@ -467,22 +468,22 @@ class Optional : private detail::OptionalCopyAssignBase<Value> {
467468

468469
/// Get the value. Must have value.
469470
/// @methodset Getters
470-
constexpr const Value& value() const& {
471+
constexpr const Value& value() const& [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
471472
require_value();
472473
return this->storage_.value;
473474
}
474475

475-
constexpr Value& value() & {
476+
constexpr Value& value() & [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
476477
require_value();
477478
return this->storage_.value;
478479
}
479480

480-
constexpr Value&& value() && {
481+
constexpr Value&& value() && [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
481482
require_value();
482483
return std::move(this->storage_.value);
483484
}
484485

485-
constexpr const Value&& value() const&& {
486+
constexpr const Value&& value() const&& [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
486487
require_value();
487488
return std::move(this->storage_.value);
488489
}
@@ -511,15 +512,28 @@ class Optional : private detail::OptionalCopyAssignBase<Value> {
511512

512513
/// Get the value. Must have value.
513514
/// @methodset Getters
514-
constexpr const Value& operator*() const& { return value(); }
515-
constexpr Value& operator*() & { return value(); }
516-
constexpr const Value&& operator*() const&& { return std::move(value()); }
517-
constexpr Value&& operator*() && { return std::move(value()); }
515+
constexpr const Value& operator*() const& [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
516+
return value();
517+
}
518+
constexpr Value& operator*() & [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
519+
return value();
520+
}
521+
constexpr const Value&& operator*()
522+
const&& [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
523+
return std::move(value());
524+
}
525+
constexpr Value&& operator*() && [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
526+
return std::move(value());
527+
}
518528

519529
/// Get the value. Must have value.
520530
/// @methodset Getters
521-
constexpr const Value* operator->() const { return &value(); }
522-
constexpr Value* operator->() { return &value(); }
531+
constexpr const Value* operator->() const [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
532+
return &value();
533+
}
534+
constexpr Value* operator->() [[FOLLY_ATTR_CLANG_LIFETIMEBOUND]] {
535+
return &value();
536+
}
523537

524538
/// Return a copy of the value if set, or a given default if not.
525539
/// @methodset Getters

0 commit comments

Comments
 (0)