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.
15 Responses to “if (n00b); warning”
Leave a Reply
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | Dec » | |||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | ||
Links
Archives
- June 2008
- April 2008
- May 2007
- January 2007
- December 2006
- June 2006
- April 2006
- March 2006
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- May 2005
- April 2005
- March 2005
June 1st, 2006 at 13:31
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.
June 1st, 2006 at 13:39
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.
June 1st, 2006 at 15:18
Yes!:
if (someFlag && doSomething());
is like:
doSomething if (someFlag)
in Perl.
June 1st, 2006 at 15:26
It can. One of the myriad -W options to gc will turn it on, can’t remember which one though :/
June 1st, 2006 at 16:12
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
June 1st, 2006 at 17:45
It can. Assuming you’re using gcc, use -Wextra (a.k.a -W).
June 1st, 2006 at 19:33
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)).
June 1st, 2006 at 20:24
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’.
June 1st, 2006 at 20:39
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
June 1st, 2006 at 22:41
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.
June 1st, 2006 at 23:13
gcc -Wall -Wextra warns about this construction.
June 2nd, 2006 at 00:02
Well, you could do if(foo() && bar()) or if(foo() || bar()), and maybe you couldn’t do that with a standalone statement?
June 2nd, 2006 at 00:56
-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.
June 2nd, 2006 at 01:16
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.
June 2nd, 2006 at 02:39
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.