Commit 7919e65c authored by Shiva Shankar P's avatar Shiva Shankar P Committed by Facebook Github Bot

Mark folly::makeGuard with 'nodiscard' to catch incorrect usage

Summary: In most cases, it would be incorrect to discard the guard immediately after its creation.

Reviewed By: yfeldblum

Differential Revision: D12911100

fbshipit-source-id: cb11b01e2d246a5d0b6bb0031968551796e25abf
parent 24e82d68
...@@ -181,7 +181,7 @@ using ScopeGuardImplDecay = ScopeGuardImpl<typename std::decay<F>::type, INE>; ...@@ -181,7 +181,7 @@ using ScopeGuardImplDecay = ScopeGuardImpl<typename std::decay<F>::type, INE>;
* http://www.codeproject.com/KB/cpp/scope_guard.aspx * http://www.codeproject.com/KB/cpp/scope_guard.aspx
*/ */
template <typename F> template <typename F>
detail::ScopeGuardImplDecay<F, true> makeGuard(F&& f) noexcept( FOLLY_NODISCARD detail::ScopeGuardImplDecay<F, true> makeGuard(F&& f) noexcept(
noexcept(detail::ScopeGuardImplDecay<F, true>(static_cast<F&&>(f)))) { noexcept(detail::ScopeGuardImplDecay<F, true>(static_cast<F&&>(f)))) {
return detail::ScopeGuardImplDecay<F, true>(static_cast<F&&>(f)); return detail::ScopeGuardImplDecay<F, true>(static_cast<F&&>(f));
} }
......
...@@ -132,7 +132,8 @@ TEST(ScopeGuard, DifferentWaysToBind) { ...@@ -132,7 +132,8 @@ TEST(ScopeGuard, DifferentWaysToBind) {
TEST(ScopeGuard, GuardException) { TEST(ScopeGuard, GuardException) {
EXPECT_DEATH( EXPECT_DEATH(
makeGuard([] { throw std::runtime_error("dtors should never throw!"); }), (void)makeGuard(
[] { throw std::runtime_error("dtors should never throw!"); }),
"dtors should never throw!"); "dtors should never throw!");
} }
...@@ -326,6 +327,6 @@ TEST(ScopeGuard, TEST_THROWING_CLEANUP_ACTION) { ...@@ -326,6 +327,6 @@ TEST(ScopeGuard, TEST_THROWING_CLEANUP_ACTION) {
}; };
int scopeExitExecuted = 0; int scopeExitExecuted = 0;
ThrowingCleanupAction onExit(scopeExitExecuted); ThrowingCleanupAction onExit(scopeExitExecuted);
EXPECT_THROW(makeGuard(onExit), std::runtime_error); EXPECT_THROW((void)makeGuard(onExit), std::runtime_error);
EXPECT_EQ(scopeExitExecuted, 1); EXPECT_EQ(scopeExitExecuted, 1);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment