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 |
---|---|---|---|---|---|---|
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
- April 2024
- February 2024
- March 2023
- November 2022
- May 2022
- February 2022
- June 2021
- January 2021
- August 2019
- October 2018
- July 2017
- May 2010
- October 2009
- August 2009
- July 2009
- March 2009
- January 2009
- July 2008
- 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
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.
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.
Yes!:
if (someFlag && doSomething());
is like:
doSomething if (someFlag)
in Perl.
It can. One of the myriad -W options to gc will turn it on, can’t remember which one though :/
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
It can. Assuming you’re using gcc, use -Wextra (a.k.a -W).
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)).
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’.
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
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.
gcc -Wall -Wextra warns about this construction.
Well, you could do if(foo() && bar()) or if(foo() || bar()), and maybe you couldn’t do that with a standalone statement?
-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.
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.
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.