diff --git a/src/common/Assert.h b/src/common/Assert.h index 71d730e99b..3ba7fa2dfe 100644 --- a/src/common/Assert.h +++ b/src/common/Assert.h @@ -55,7 +55,7 @@ # else # define NXT_ASSERT_CALLSITE_HELPER(file, func, line, condition) \ do { \ - (void)sizeof(condition); \ + NXT_UNUSED(sizeof(condition)); \ } while (NXT_ASSERT_LOOP_CONDITION) # endif #endif diff --git a/src/common/Compiler.h b/src/common/Compiler.h index 608ad77b3a..36ddd39c4c 100644 --- a/src/common/Compiler.h +++ b/src/common/Compiler.h @@ -22,6 +22,7 @@ // - NXT_NO_DISCARD: An attribute that is C++17 [[nodiscard]] where available // - NXT_(UN)?LIKELY(EXPR): Where available, hints the compiler that the expression will be true // (resp. false) to help it generate code that leads to better branch prediction. +// - NXT_UNUSED(EXPR): Prevents unused variable/expression warnings on EXPR. // Clang and GCC #if defined(__GNUC__) @@ -72,6 +73,9 @@ extern void __cdecl __debugbreak(void); # error "Unsupported compiler" #endif +// It seems that (void) EXPR works on all compilers to silence the unused variable warning. +#define NXT_UNUSED(EXPR) (void)EXPR + // Add noop replacements for macros for features that aren't supported by the compiler. #if !defined(NXT_LIKELY) # define NXT_LIKELY(X) X diff --git a/src/tests/unittests/BitSetIteratorTests.cpp b/src/tests/unittests/BitSetIteratorTests.cpp index 4ea7b680be..50198a9ac8 100644 --- a/src/tests/unittests/BitSetIteratorTests.cpp +++ b/src/tests/unittests/BitSetIteratorTests.cpp @@ -51,7 +51,7 @@ TEST_F(BitSetIteratorTest, EmptySet) { // causing an unreachable code warning in MSVS bool sawBit = false; for (unsigned long bit : IterateBitSet(mStateBits)) { - (void) bit; + NXT_UNUSED(bit); sawBit = true; } EXPECT_FALSE(sawBit); diff --git a/src/tests/unittests/ErrorTests.cpp b/src/tests/unittests/ErrorTests.cpp index 7320ad9192..26af0846b2 100644 --- a/src/tests/unittests/ErrorTests.cpp +++ b/src/tests/unittests/ErrorTests.cpp @@ -178,7 +178,7 @@ TEST(ErrorTests, TRY_RESULT_Error) { auto Try = [ReturnError]() -> ResultOrError { int* result = nullptr; NXT_TRY_ASSIGN(result, ReturnError()); - (void) result; + NXT_UNUSED(result); // NXT_TRY should return before this point EXPECT_FALSE(true); @@ -233,7 +233,7 @@ TEST(ErrorTests, TRY_RESULT_ConversionToError) { auto Try = [ReturnError]() -> MaybeError { int* result = nullptr; NXT_TRY_ASSIGN(result, ReturnError()); - (void) result; + NXT_UNUSED(result); return {}; }; diff --git a/src/tests/unittests/SerialQueueTests.cpp b/src/tests/unittests/SerialQueueTests.cpp index 7f0ca3d6bf..126b3a4a5c 100644 --- a/src/tests/unittests/SerialQueueTests.cpp +++ b/src/tests/unittests/SerialQueueTests.cpp @@ -27,7 +27,7 @@ TEST(SerialQueue, BasicTest) { // Iterating on empty queue 1) works 2) doesn't produce any values for (int value : queue.IterateAll()) { - (void) value; + NXT_UNUSED(value); ASSERT_TRUE(false); } @@ -50,7 +50,7 @@ TEST(SerialQueue, BasicTest) { ASSERT_TRUE(queue.Empty()); for (int value : queue.IterateAll()) { - (void) value; + NXT_UNUSED(value); ASSERT_TRUE(false); } }