[libcxx-commits] [libcxx] d7e0cec - [libc++][test] Mark `test_comparisons.h` helpers as nodiscard

Adrian Vogelsgesang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 12 03:30:49 PDT 2022


Author: Adrian Vogelsgesang
Date: 2022-08-12T03:30:33-07:00
New Revision: d7e0cec60ee3f6f3bfa60764ec1022a4072fe852

URL: https://github.com/llvm/llvm-project/commit/d7e0cec60ee3f6f3bfa60764ec1022a4072fe852
DIFF: https://github.com/llvm/llvm-project/commit/d7e0cec60ee3f6f3bfa60764ec1022a4072fe852.diff

LOG: [libc++][test] Mark `test_comparisons.h` helpers as nodiscard

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/test/support/test_comparisons.h b/libcxx/test/support/test_comparisons.h
index 8401c920450c5..e4bb4196543b6 100644
--- a/libcxx/test/support/test_comparisons.h
+++ b/libcxx/test/support/test_comparisons.h
@@ -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;
 

diff  --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index e8539cbe6d338..6bea8f12f3d01 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -235,6 +235,12 @@
 #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 {


        


More information about the libcxx-commits mailing list