Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wunused-but-set-variable warning treatment

Tags:

c

gcc

gcc-warning

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

warning: variable ‘status’ 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 _DEBUG_ flag isn't set I get the warning. Any ideas how can I workaround this warning?

like image 817
alnet Avatar asked Jul 05 '11 13:07

alnet


People also ask

How do I turn off Wunused variable?

You can then use -Wunused-variable to turn on that warning specifically, without enabling others. To disable that warning, replace the leading -W with -Wno- , and use it in combination with an option to enable the desired warning level.

How does GCC treat warning errors?

The warning is emitted only with --coverage enabled. By default, this warning is enabled and is treated as an error. -Wno-coverage-invalid-line-number can be used to disable the warning or -Wno-error=coverage-invalid-line-number can be used to disable the error.

How do I disable GCC?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.


2 Answers

You can change your ASSERT macro to:

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

If the expression has no sideeffects, then it should still be optimised out, but it should also suppress the warning (if the expression does have side-effects, then you would get different results in debug and non-debug builds, which you don't want either!).

like image 144
caf Avatar answered Oct 04 '22 06:10

caf


The compiler option to turn off unused variable warnings is -Wno-unused. To get the same effect on a more granular level you can use diagnostic pragmas like this:

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

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

like image 27
pmr Avatar answered Oct 04 '22 06:10

pmr