6

Is there any flag in GCC (like -Wempty-body in clang), that could help me detect semicolons after the braces of while/for loops? Sometimes it is very hard for humans to find these simple mistakes.

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

I use GCC 4.7.3 and clang 3.2-1~exp9ubuntu1.

Edited: I also check if compilers could help me find these mistakes after "if-else statements".

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

What is interesting gcc is more helpful than clang (with this flags (-Wall -pedantic -Wempty-body) by printing warning:

main.cpp:30:9: warning: suggest braces around empty body in an ‘else’ statement [-Wempty-body]
NiegodziwyBeru
  • 864
  • 1
  • 10
  • 19
  • possible duplicate of [Can gcc accurately catch useless conditionals?](http://stackoverflow.com/questions/851162/can-gcc-accurately-catch-useless-conditionals) – Oliver Charlesworth Jun 15 '13 at 19:18
  • 1
    On second thoughts, that's not a duplicate, apologies. However, isn't `-Wempty-body` also a GCC flag? – Oliver Charlesworth Jun 15 '13 at 19:20
  • 6
    This seems to me a very good reason (i.e. other than style) to put starting `{` on the same line as the `if`/`for`/`while` etc. And to have one-liners on the same line as well. – Kninnug Jun 15 '13 at 19:29
  • @OliCharlesworth The GCC flag doesn't cover empty for or while loop. From [here](http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html) `Warn if an empty body occurs in an ‘if’, ‘else’ or ‘do while’ statement. This warning is also enabled by -Wextra. ` – FDinoff Jun 15 '13 at 19:30
  • 1
    I started writing an answer before I'd done full research, but it seems like the statement "like `-Wempty-body` in clang" is a bit misleading. Neither of my versions of g++ (4.6.3) or clang++ (2.9) gives a warning when using `-Wempty-body`. – Mats Petersson Jun 15 '13 at 22:58
  • The loop body is not empty, it is `++i;` – Ben Voigt Jun 15 '13 at 23:05
  • @BenVoigt: "Bastardizing" the for-statement to not have `++i`, but instead doing ` for (i = 0; i++ < 10;);` [yes, I know, it doesn't do the same thing any longer- but this was just to test if it "works"] still doesn't give any warning. – Mats Petersson Jun 15 '13 at 23:07
  • 1
    Apparently there were too many false positives, and people started arguing how to make presence of a comment or macro (expanding to nothing) inhibit the warning, and at that point the whole idea was just scrapped. http://gcc.gnu.org/ml/gcc-patches/2008-11/msg00361.html – Ben Voigt Jun 15 '13 at 23:12
  • @MatsPetersson I'm using `Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)`. – NiegodziwyBeru Jun 20 '13 at 19:55
  • @Kninnug Everything is good until you make simple mistake. There is flag in gcc `-Weffc++` that help you check style guidelines based on Scott Meyers Effective C++. I would be good if compilers similarly could help me with this `;` and suggest to use empty brackets. – NiegodziwyBeru Jun 20 '13 at 20:26

2 Answers2

2

try

$gcc -Wempty-body foo.c

or

gcc -Wextra -c foo.c

Abhas Tandon
  • 1,859
  • 16
  • 26
1

The obvious answer here is "compile your code with clang++" (although my 2.9 version for x86-64 doesn't seem to catch this particular problem, just like gcc 4.6.3 doesn't catch it - so I'm not entirely convinced the original premise of the question is valid).

This particular code can avoid this problem by using the form, by giving an error for using i after the for-loop itself:

for(int i = ...) 

instead of

int i;
for(i = ...)

Of course, that doesn't work in the case where you want i to have a value after the loop.

[And yes, it's a very annoying error - I've spent several hours staring at the screen to find this sort of bug at times - other times you spot it immediately!]

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227