Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Warning code for catching semicolon after a while loop

nielsen__jan_kampman
418 Views

Hello all.

I wonder if there is a warning code which

flags that there is placed an unintentional semicolon 

after a while loop.

while(condition);

{

  Body

}

I have already sought after the warning code my

self in the documentation and have written

code trying to provoke a warning but without

any luck. I have enabled all warnings but to

no avail. The compiler is Intel C++ 2019.

If there is no warning code for this, my vain

hope is that Intel will in time add an warning

for this, because I cannot be the first or the last

to have this kind of bug.

Please be kind it is my first posting in this

forum. Hope my attempt at writing in English

is understandable.

 

Best regards

Jan, DK. 

0 Kudos
5 Replies
jimdempseyatthecove
Honored Contributor III
418 Views

while(condition); is a valid syntax for C/C++. This said, there are several "lint" programs available that flag potential problems in coding. In this case enforcing:    while(condition) continue;

Perform a Google search on "C++ lint tools" to locate potential tools for you to use.

Jim Dempsey

0 Kudos
nielsen__jan_kampman
418 Views

Hello Jim

Thank you for your answer.

I am reading Microsofts "Writing solid code" By Steve Maguire.

I know this is a valid syntax and I could buy a affordable Lint, but I feel

it is something the compiler should be able to handle. So far as I know

it is a common mistake just like : if (Number = 0) instead of if (Number == 0)

Basically the idea is to warn about those constructs in the syntax that is valid

but rarely used and if you really want to, you could write : while(condition); {}

to supres the warning(the books example). One of the points is to let the compiler

find errors, that are not illegal but introduces logical errors, during compilation - which is an idea I find useful.

 

Best regards 

 

Jan

 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
418 Views

>>common mistake just like : if (Number = 0) instead of if (Number == 0)

Consider if(Number = OtherNumber), or if(result = expression)

In the first case, the equate may be as intended. In the second case equate is likely intended, as the compiler cannot disambiguate with if(exitValue = expression).

This said, I can see it could be handy to have a -warn:ambiguous_if, -warn:ambiguous_while

>>while(condition); {}

IMHO, the proper ways are:

while(condition) continue;
while(condition) {};     // semicolon to right of {}

Jim Dempsey

 

0 Kudos
nielsen__jan_kampman
418 Views

Hello Again

There is a warning by default(Visual Studio 17, C++ 2019)

for if (Number = 0). I have chosen to treat it like an error with

the help of these options : /Qdiag-enable:187  /Qdiag-error:187 

I have managed to generate automatically a list

of all warnings, but I have not found the right search word

to locate the warning for while(condition);

 

Best regards

Jan

 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
418 Views

The while(condition); is not an error in syntax.

Consider a multi-threaded scenario where while(!Done.load()); is used as a barrier.

In the single threaded situation where while(condition); is a programming error, this will quickly evident when debugging the program.

Jim Dempsey

0 Kudos
Reply