How can I make the compiler warn me about this?
Using Release 1.7 Build 6 with Atmel AT91SAM7 Support Package
Is there a compiler option that would warn me about the code below? I meant to use || instead of && obviously, but I was surprised that I didn't get a warning about it. I looked through the -W options and found -Wunreachable-code, but it didn't catch it. Any suggestions?
if (var == 0 && var == 1)
{
...
}
-
If you have defined var as volatile, it might change its value between both evaluations. The compiler must not warn here!
If var is not volatile (or even const), the compiler may reduce the code and remove the whole if construct silently.
This is something the compiler does quite often, if optimization settings allow it - and it is expected to do so!
If you'd expect it to warn here, it would issue a flood of warnings, thus hiding the important messages.
If you want such comfortable code inspection, engage "lint" or "pclint", which will provide exactly what you want:
That tool would tell you that your condition is never valid or always valid or merely bad coding style, etc.
thus improving code quality.IMHO the compiler shall compile what I tell him by source code, and it shall not worry about why I code this way.
It should only complain, if it could not produce a compilation implementing exactly what the source code describes.
-
If you have defined var as volatile, it might change its value between both evaluations. The compiler must not warn here!
Bernhard,
Why would, I the programmer want to do something so dumb????? If we are looking for something to "edge" this is the most unreliable way you could do this. If this bad code were in my project anywhere I would want to know about it.
So Michael,
Lint is you solution you are looking for, go look it up. And always strive for bug free code.
Regards,
Michael Freeman
Principal Design Engineer
Update System, Inc.
Please sign in to leave a comment.
Comments
2 comments