Warning handling using Wunused-but-set-variable

I have the following code, and compiling it with gcc-4.6, I get a warning:

warning: the value of the variable is set but not used [-Wunused-but-set-variable]

#if defined (_DEBUG_) #define ASSERT assert #else /* _DEBUG_ */ #define ASSERT( __exp__ ) #endif static inline void cl_plock(cl_plock_t * const p_lock) { status_t status; ASSERT(p_lock); ASSERT(p_lock->state == INITIALIZED); status = pthread_rwlock_unlock(&p_lock->lock); ASSERT(status == 0); } 

When the _DEBUG_ flag is not set. I get a warning. Any ideas how I can get around this warning?

+5
source share
3 answers

You can change your ASSERT macro to:

 #if defined (_DEBUG_) #define ASSERT assert #else /* _DEBUG_ */ #define ASSERT( exp ) ((void)(exp)) #endif 

If an expression has no side effects, it should be optimized, but it should also suppress the warning (if the expression has side effects, then you will get different results in debug and non-debug builds that you also donโ€™t want!).

+3
source

The compiler option to disable warnings about unused variables is -Wno-unused . To get the same effect at a more granular level, you can use diagnostic pragmas as follows:

 int main() { #pragma GCC diagnostic ignored "-Wunused-variable" int a; #pragma GCC diagnostic pop // -Wunused-variable is on again return 0; } 

This, of course, is not portable, but you can use something similar for VS.

+2
source

You can combine the declaration of the status variable with the #ifdef .

 #ifdef _DEBUG_ status_t status #endif 

EDIT: You should also surround the call:

 #ifdef _DEBUG_ status = pthread_rwlock_unlock(&p_lock->lock); #else pthread_rwlock_unlock(&p_lock->lock); #endif 

or you can disable the error message.

+1
source

Source: https://habr.com/ru/post/895372/


All Articles