How can I make the compiler warn me about this?

Comments

2 comments

  • Avatar
    Bernhard Holzmayer

    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.

    0
    Comment actions Permalink
  • Avatar
    Michael Freeman

    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.

    0
    Comment actions Permalink

Please sign in to leave a comment.