Important: Please read the Qt Code of Conduct -

Set but not used

  • Hi, All

    In the following code, on line 8, the
    "Valiable 'rdata' set but not used", I get a warning.
    Then when I delete the comment out, the warning goes away.
    Why is this?

    Thank you.

     1	void MainWindow::on_pushButton_clicked()
     2	{
     3	    unsigned char data[4];
     4	        data[0] = 0xA6;
     5	        data[1] = 0x90;
     6	        data[2] = 0x21;
     7	        data[3] = 0x02;
     8	    unsigned char rdata[4];
     9	    unsigned long ret = 0x0;
    11	    for(int i=0; i<4; i++){
    12	        ret = ret << 8;
    13	        ret = ret | data[i];
    14	    }
    16	    qDebug() << QString::number(ret, 16);
    18	    for(int i=0; i<4; i++){
    19	        rdata[i] = ret & 0xff;
    20	        ret = ret >> 8;
    21	    }
    23	//    for(int i=0; i<4; i++){
    24	//        qDebug() << QString::number(rdata[i], 16);
    25	//    }
    26	}

  • @taku-s
    Get a warning from what? The compiler when compiling? From within Qt Creator where clang is being used to parse while you edit? Then it can be mistaken, which does happen. Your existing code does indeed use rdata. Unless the compiler is so clever as to notice that all you are doing is storing into a local array which you never then use/access, unless you have the qDebug() output line? Because if you think about your assigments into the rdata array don't actually achieve anything... The error message does look as though it has noticed this....!

  • I'm not sure I can be more clear than your compiler but I'll try. That warning comes up if your variable only ever appears on the left hand side of an =. When you uncomment the last part it shows up as an argument to a function call QDebug::operator<< so your compiler is happy

  • @VRonin said in Set but not used:

    That warning comes up if your variable only ever appears on the left hand side of an =

    Over the years I am not aware of ever having noticed that a compiler gives this warning against array element assignments. MSVC. Is this user/warning gcc?

  • @JonB I use gcc/mingw, those warnings about var not being used (they apply also to unused method params) are here for quite some time already.

  • @artwaw
    Hmm. Of course about unused individual variables/parameters, I can only say that in many years of MSVC I have never (don't think so anyway) seen one where it realizes that the whole array is never used if you only assign to its elements.... BTW I assume this can only happen because the array elements are a simple type, so it can guarantee no side-effects.

    BTW: while you are developing the assignments on the array, but are not yet ready to write the code to read from it, you should be able to put in UNUSED(rdata). I do, but now I'm not sure where I get this macro from! I think it comes with gcc, but now I can't find a refence to it? Or do I actually use Qt's Q_UNUSED()? :) OK, use that one! (And ignore what the docs say about "the parameter with the specified name is not used in the body of a function", it applies equally to non-parameter local variables.

  • Thank you all for your responses.
    I wasn't sure I might have made some rudimentary mistakes myself, but I'm relieved.

    I'm using both Linux + GCC,GDB and Windows 10 + MSVC2019 32bit.
    The phenomenon I asked about is occurring on Linux + GCC. This one works fine, although I get a warning.
    In fact, it's the Windows 10+MSVC one that I'm really struggling with.
    In this case, while debugging, when I enter the for statement, starting on line 11, the imediate window that was normally displayed before disappears.
    Is there any solution to this?

    Best regards, please.

  • C++ doesn't really do "immediate". The compiler might hack simple code to let you use it but as a general rule it's not possible to use C++ as an interpreted language

Log in to reply