Semicolon detection after / loop if brackets

Is there any flag in GCC (e.g. -Wempty-body in clang) that can help me define semicolons after while / for curly braces? Sometimes it’s very difficult for people to find these simple mistakes.

 int i = 0; for (i = 0; i < 10; ++i); { cout << i << endl; } 

I am using GCC 4.7.3 and clang 3.2-1 ~ exp9ubuntu1.

Edited: I also check to see if compilers can help me find these errors after the words "if-else".

 if (i == 0) { cout << i << endl; } else; { cout << i << endl; } 

Interestingly, gcc is more useful than clang (with these flags ( -Wall -pedantic -Wempty-body ), printing a warning:

 main.cpp:30:9: warning: suggest braces around empty body in an 'else' statement [-Wempty-body] 
+6
source share
2 answers

to try

$ gcc -Wempty-body foo.c

or

gcc -extra -c foo.c

+2
source

The obvious answer here is "compile your code using clang ++" (although my version 2.9 for x86-64 does not seem to catch this particular problem, just as gcc 4.6.3 does not catch it - so I'm not quite convinced that the original premise of the question is correct).

This specific code can avoid this problem by using the form by providing an error for using i after the for -loop itself:

 for(int i = ...) 

instead

 int i; for(i = ...) 

Of course, this does not work if you want i have a value after the loop.

[And yes, this is a very unpleasant mistake - I looked at the screen for several hours to find such an error at times - sometimes you noticed it right away!)

+1
source

Source: https://habr.com/ru/post/947373/


All Articles