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 Robert McQueen @ 12:05 pm
.:. Trackback .:. Permalink

15 Responses to “if (n00b); warning”

  1. Pádraig Brady Says:

    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)
    ;
    else
    {
    stuff;
    }

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

  3. Someone Says:

    Yes!:

    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
    chain

    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. Ben Hutchings Says:

    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)
    some_long_line();
    else
    some_other_long_line();

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

    if (foo)
    ;//some_long_line();
    else
    some_other_long_line();

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

Leave a Reply