any g++ compilation warning to detect empty if statement

L

lasing

Hi all,

recently encounter following bugs:

if (a > b); //<- bug in the careless ";" here
return 0;

Due to the extra ";" in the if statement line, it always return 0.

Anyone know g++ has any compiler - warning flag to detect this kind of
error? if not, is it any source code checking tools can do it ?

thanks,
Eric.
 
V

Victor Bazarov

lasing said:
recently encounter following bugs:

if (a > b); //<- bug in the careless ";" here
return 0;

Due to the extra ";" in the if statement line, it always return 0.

Anyone know g++ has any compiler - warning flag to detect this kind of
error?

You need to ask in the compiler newsgroup about the settings for
any particular compiler. This is not a C++ language question.
if not, is it any source code checking tools can do it ?

I bet PC-lint can do it. [I just checked, it does: Info 721]
http://gimpel-online.com/MsgRef.html#721

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi all,

recently encounter following bugs:

if (a > b); //<- bug in the careless ";" here
return 0;

Due to the extra ";" in the if statement line, it always return 0.

Anyone know g++ has any compiler - warning flag to detect this kind of
error? if not, is it any source code checking tools can do it ?

Not really what you asked for, but if your editor can indent the files
you work on, use that function, 'return 0;' will then be on the same
level as the if-statement which should make you suspicious.
 
F

Frank Birbacher

Hi!
Anyone know g++ has any compiler - warning flag to detect this kind of
error? if not, is it any source code checking tools can do it ?

My gcc manual tells me it is covered by "-Wextra". But in practice my
g++ 4.1.2 does not complain about it -.-

Frank
 
B

BobR

lasing said:
Hi all,

recently encounter following bugs:

if (a > b); file://<- bug in the careless ";" here
return 0;

Due to the extra ";" in the if statement line, it always return 0.

Anyone know g++ has any compiler - warning flag to detect this kind of
error? if not, is it any source code checking tools can do it ?
thanks, Eric.

Change your bad habits. If you get in the habit of always using
curly-braces, you will save yourself some of the 'gotchas'.

if( b ){}
if( a > b ){ return 0; }
if( a > b ){} else{ /* stuff */ }
for( /* .... */ ){}
for( /* .... */ ){ /* stuff */ }
while( a > b ){ /* stuff */; ++b; }
while( /* something */ ){ ; } // if that's what you intended.
etc.
 
V

Victor Bazarov

BobR said:
Change your bad habits. If you get in the habit of always using
curly-braces, you will save yourself some of the 'gotchas'.

if( b ){}
if( a > b ){ return 0; }
if( a > b ){} else{ /* stuff */ }
for( /* .... */ ){}
for( /* .... */ ){ /* stuff */ }
while( a > b ){ /* stuff */; ++b; }
while( /* something */ ){ ; } // if that's what you intended.
etc.

How does that help?

if (somefunkycondition);
{
/* plenty of stuff in curly braces */
}

V
 
B

BobR

Victor Bazarov said:
How does that help?

if (somefunkycondition);
{
/* plenty of stuff in curly braces */
}

So sorry I tried to suggest something. I'll refrain from such in the future!

[ Beam me up Scottie! There is no intelligent life on Earth. ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top