开发者

warning: variable set but not used [-Wunused-but-set-variable]

I have been getting following warning while compiling the C source code in the gcc 4.6.1.

   warning: variable set but not used [-Wunused-but-set-variable]

I refered to this link Wunused but could get exactly what is causing this warning.Would anybody tell me in more details what is causing this warning and how can We get rid of it?

[EDIT] I have a following snippet of code. The compile shows the above mentioned warning. Could you please suggest me how can correct it?

   test_func开发者_如何学Pythontion(){
   BOOL BoolTest;
   BoolTest = test_fucntion2();

   #ifdef CHECK
   if (!BoolTest) {
   misc_StartErrorReport();
   misc_ErrorReport("\n test_function2: Input not indexed.\n");
   misc_FinishErrorReport();
          }
   #endif
   // 
    BoolTest is no more used below it.
   // } 


You need to include the preprocessor guards around the declaration and initialisation of BoolTest:

test_function()
{
#ifdef CHECK
    BOOL BoolTest = test_function2();
#else
    test_function2();
#endif


#ifdef CHECK
    if (!BoolTest) {
        misc_StartErrorReport();
        misc_ErrorReport("\n test_function2: Input not indexed.\n");
        misc_FinishErrorReport();
    }
#endif

(this assumes that you still want to call test_function2() even if CHECK is not defined, presumably for its side-effects - if not, then you don't need the #else section and you can combine the two #ifdef blocks into one).


Setting a variable is assigning it a value (maybe implicitly)

int main(void) {
    int local1, local2;
    local1 = 0; /* local1 set to 0 */
    local2 = 0; /* local2 set to 0 */
    return 0;
}

In the program above, both variables were set to a value but they weren't used. If I replace the second line with

    int local2 = local1;

now I have used the local1 variable -- and the warnings should be only 1.

To get rid of the warning, delete the assignment from your code. This may, in turn create other warnings ... :)


It means that you assign a value to a variable, but then you never read that value later in your code (hence the verbage, "set but not used"). For example:

int useful = 10;
int useless = 3;
if (useful) {
    //Do stuff
}

Notice that you give both useful and useless values, but you only read the value in useful. Usually, when I get this message it means that I forgot about a variable or found a way to inline a statement that no longer needs that variable.


With g++ 7.x and higher and clang++ 4.x and higher (using c++11 or higher), as well as with Visual Studio 2017 version 15.3 and later (available with /std:c++17), you can use the standarized [[maybe_unused]] attribute.

For example,

int main()
{
    int x [[maybe_unused]] = 5;
}

will not give a warning, not even with -Wunused-variable and the likes.


https://gcc.gnu.org/gcc-4.6/porting_to.html

New warnings for unused variables and parameters

The behavior of -Wall has changed and now includes the new warning flags -Wunused-but-set-variable and (with -Wall -Wextra) -Wunused-but-set-parameter. This may result in new warnings in code that compiled cleanly with previous versions of GCC.

For example,

void fn (void) { int foo; foo = bar (); /* foo is never used. */ }

Gives the following diagnostic:

warning: variable "foo" set but not used [-Wunused-but-set-variable]

Although these warnings will not result in compilation failure, often -Wall is used in conjunction with -Werror and as a result, new warnings are turned into new errors.

To fix, first see if the unused variable or parameter can be removed without changing the result or logic of the surrounding code. If not, annotate it with __attribute__((__unused__)).

As a workaround, add -Wno-error=unused-but-set-variable or -Wno-error=unused-but-set-parameter.


You have not used BoolTest. You can see there is no difference between your code and

test_function(){
#ifdef CHECK
if (!test_fucntion2()) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜