diff options
author | Victor Zverovich <victor.zverovich@gmail.com> | 2019-11-30 07:52:33 -0800 |
---|---|---|
committer | Victor Zverovich <victor.zverovich@gmail.com> | 2019-11-30 07:52:33 -0800 |
commit | 6037b3cae9d2fdac32b2124b5c392a33e71fad46 (patch) | |
tree | b29722effc60d2e4c6e1a097ded447c9f2f2f259 | |
parent | fafb03fa6d764f3cedf80e222a1e5998b80ef79c (diff) | |
download | fmtlib-6037b3cae9d2fdac32b2124b5c392a33e71fad46.tar.gz |
Fix dangling else problem in FMT_ASSERT
-rw-r--r-- | include/fmt/core.h | 5 | ||||
-rw-r--r-- | test/assert-test.cc | 11 |
2 files changed, 14 insertions, 2 deletions
diff --git a/include/fmt/core.h b/include/fmt/core.h index 346f7616..14ab40f7 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -231,8 +231,9 @@ void assert_fail(const char* file, int line, const char* message); # define FMT_ASSERT(condition, message) # else # define FMT_ASSERT(condition, message) \ - if (!(condition)) \ - fmt::internal::assert_fail(__FILE__, __LINE__, (message)) + ((condition) \ + ? void() \ + : fmt::internal::assert_fail(__FILE__, __LINE__, (message))) # endif #endif diff --git a/test/assert-test.cc b/test/assert-test.cc index 0679cee6..26a87a7b 100644 --- a/test/assert-test.cc +++ b/test/assert-test.cc @@ -20,3 +20,14 @@ TEST(AssertTest, Fail) { EXPECT_DEBUG_DEATH_IF_SUPPORTED(FMT_ASSERT(false, "don't panic!"), "don't panic!"); } + +bool test_condition = false; + +TEST(AssertTest, DanglingElse) { + bool executed_else = false; + if (test_condition) + FMT_ASSERT(true, ""); + else + executed_else = true; + EXPECT_TRUE(executed_else); +} |