[libc++][test] Mark `test_comparisons.h` helpers as nodiscard
authorAdrian Vogelsgesang <avogelsgesang@tableau.com>
Sun, 7 Aug 2022 17:29:32 +0000 (10:29 -0700)
committerAdrian Vogelsgesang <avogelsgesang@salesforce.com>
Fri, 12 Aug 2022 10:30:33 +0000 (03:30 -0700)
I accidentally wrote `testComparisons(...)` instead of
`assert(testComparisons(...))`. This compiled without issues, but
did not provide the intended test coverage. By adding a `nodiscard`,
we can make sure that the compiler catches such mistakes for us.

Differential Revision: https://reviews.llvm.org/D131364

libcxx/test/support/test_comparisons.h
libcxx/test/support/test_macros.h

index 8401c92..e4bb419 100644 (file)
@@ -30,7 +30,7 @@
 
 //  Test all six comparison operations for sanity
 template <class T, class U = T>
-TEST_CONSTEXPR_CXX14 bool testComparisons(const T& t1, const U& t2, bool isEqual, bool isLess)
+TEST_NODISCARD TEST_CONSTEXPR_CXX14 bool testComparisons(const T& t1, const U& t2, bool isEqual, bool isLess)
 {
     assert(!(isEqual && isLess) && "isEqual and isLess cannot be both true");
     if (isEqual)
@@ -84,7 +84,7 @@ TEST_CONSTEXPR_CXX14 bool testComparisons(const T& t1, const U& t2, bool isEqual
 
 //  Easy call when you can init from something already comparable.
 template <class T, class Param>
-TEST_CONSTEXPR_CXX14 bool testComparisonsValues(Param val1, Param val2)
+TEST_NODISCARD TEST_CONSTEXPR_CXX14 bool testComparisonsValues(Param val1, Param val2)
 {
     const bool isEqual = val1 == val2;
     const bool isLess  = val1  < val2;
@@ -137,7 +137,7 @@ constexpr void AssertOrderReturn() {
 }
 
 template <class Order, class T, class U = T>
-constexpr bool testOrder(const T& t1, const U& t2, Order order) {
+TEST_NODISCARD constexpr bool testOrder(const T& t1, const U& t2, Order order) {
     bool equal = order == Order::equivalent;
     bool less = order == Order::less;
 
@@ -145,7 +145,7 @@ constexpr bool testOrder(const T& t1, const U& t2, Order order) {
 }
 
 template <class T, class Param>
-constexpr bool testOrderValues(Param val1, Param val2) {
+TEST_NODISCARD constexpr bool testOrderValues(Param val1, Param val2) {
   return testOrder(T(val1), T(val2), val1 <=> val2);
 }
 
@@ -153,7 +153,7 @@ constexpr bool testOrderValues(Param val1, Param val2) {
 
 //  Test all two comparison operations for sanity
 template <class T, class U = T>
-TEST_CONSTEXPR_CXX14 bool testEquality(const T& t1, const U& t2, bool isEqual)
+TEST_NODISCARD TEST_CONSTEXPR_CXX14 bool testEquality(const T& t1, const U& t2, bool isEqual)
 {
     if (isEqual)
         {
@@ -175,7 +175,7 @@ TEST_CONSTEXPR_CXX14 bool testEquality(const T& t1, const U& t2, bool isEqual)
 
 //  Easy call when you can init from something already comparable.
 template <class T, class Param>
-TEST_CONSTEXPR_CXX14 bool testEqualityValues(Param val1, Param val2)
+TEST_NODISCARD TEST_CONSTEXPR_CXX14 bool testEqualityValues(Param val1, Param val2)
 {
     const bool isEqual = val1 == val2;
 
index e8539cb..6bea8f1 100644 (file)
 #define LIBCPP_ONLY(...) static_assert(true, "")
 #endif
 
+#if __has_cpp_attribute(nodiscard)
+#  define TEST_NODISCARD [[nodiscard]]
+#else
+#  define TEST_NODISCARD
+#endif
+
 #define TEST_IGNORE_NODISCARD (void)
 
 namespace test_macros_detail {