Robotic Tendencies
The personal blog of Robert McQueen

June 1, 2006

if (n00b); warning

I wasted a non-trivial amount of time yesterday debugging code in which I’d accidentally written:

if (...);

Is there any situation where if (foo); can achieve something which just foo; couldn’t? Could the compiler not warn about a conditional that contained no code?

Aggravating lapses in competence aside, I’ve realised I’ve not blogged for months, so over the next few days I’m going to try and write a little about what I’ve been working on recently.

posted by ramcq @ 12:05 pm
Comments (15) .:. Trackback .:. Permalink

15 responses to “if (n00b); warning”

  1. That surprises me. I thought that used to be warned about?
    I’ve confirmed that 4.0.2 doesn’t give any warning
    with -Wall -pedantic.

    It’s quite easy to do this when going to and from
    shell and python code as they both use ; and :
    right after the expression.

  2. fraggle says:

    I’ve certainly code like

    if (blah)

    Sometimes I’ve done things like this because I want to later add something in where the first semicolon is.

  3. Someone says:


    if (someFlag && doSomething());

    is like:

    doSomething if (someFlag)

    in Perl.

  4. iain says:

    It can. One of the myriad -W options to gc will turn it on, can’t remember which one though :/

  5. Buck says:

    as a matter of style, it’s sometimes been useful to me to

    if (…)
    /* do nothing */;
    else if (…)
    /* do nothing */;
    else {
    /* do something */

    , especially if the (…) expressions change things that get
    evalutated in subsequent (…)’s

    that’s not saying i’m an exemplar of good style or anything

  6. It can. Assuming you’re using gcc, use -Wextra (a.k.a -W).

  7. Anonymous says:

    Unless it has an else clause, it can’t possibly have any useful effect. If it *does* have an else clause, the compiler should *still* warn about it, because you could just as easily write it as if(!(foo)).

  8. john doe says:

    If it has an else-clause, it won’t compile.

    if (a); { b }

    will evaluate a, then run b. If it was

    if (a); { b } else { c }

    the compiler will complain about a meaningless ‘else’.

  9. Peter says:

    mcs/gmcs (Mono) and csc (.NET) give me a warning “possible mistaken empty statement”.
    Adding an “else”-clause even throw out an error “Expecting `;`”

    This is C# specific, though

  10. Gunnar says:

    Well, but remember that ; is by itself the smallest valid complete statement in C. Yes, getting a warning out for this behavior can be easy – but according to the language definition, it is completely legal.

  11. slacker says:

    gcc -Wall -Wextra warns about this construction.

  12. Ari Pollak says:

    Well, you could do if(foo() && bar()) or if(foo() || bar()), and maybe you couldn’t do that with a standalone statement?

  13. Sean Neakums says:

    -Wextra catches it (tested with gcc 4.0) but it may well add so much extra spew that you miss the real problems. Alas there’s no separate -W option for it.

  14. Tack says:

    Been coding for 16 years and I also recently (within the last 6 months) spent a non-trivial amount of time debugging the exact same user error.

    Actually I spent like 2 hours trying to figure out the problem, finally gave up and went to bed around 3am, and once I woke up and looked at the code again, saw it inside of 45 seconds.

    Such is how it goes.

  15. Adam Hooper says:

    Let’s say you want had this:

    if (foo)

    And you needed to debug something. One very quick debug might give:

    if (foo)

    I personally think there should be a warning, but the above would at least be mildly useful to some people.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.