cancel
Showing results for 
Search instead for 
Did you mean: 

Avoiding the "variable set but not used" warning

EThom.3
Associate III

This may be such a basic question, that I may be embarrassing myself here. Not even sure if I've selected the best fitting location...

I like to keep a "clean slate" when compiling, that is, no warnings should be present. It makes it easier to see if a problem has occurred, if I don't need to remember how many warnings are to be expected in a certain piece of firmware. Also, if I get too comfortable with a number of warnings being present, something may slip.

Now, in microcontrollers, we sometimes need to read something from a register, without actually needing the value for anything, such as emptying an SPI receive buffer after transmitting. In some cases, I use a variable that already exists, and is in use elsewhere, but this is not always practical. And then I get the "variable set but not used" warning.

I don't want to suppress the warning type altogether, because it does come in handy now and then.

There is the possibility to declare a global variable for this purpose, as the compiler doesn't complain about unused global variables. This just seems a bit "clunky" to me.

It might also be possible to use another (unused) register as the recipient for the unneeded value. That just isn't quite practical if the code is to be ported to a different device or project. Perhaps a read-only register might work? :thinking_face:

Question: What do other people do? Is there a super-clever solution that I just don't know about?

I've searched elsewhere, but it seems that most programmers don't code for microcontrollers, and don't understand why anyone would ever read a variable without using it.

10 REPLIES 10

Now that you mention it, I'm pretty sure that it was on stackoverflow that I saw the rather rude answer.

Yet another reason to stick with the ST community.